# Exiting Middleware processing and returning a response

**URL:** <https://discourse.slimframework.com/t/exiting-middleware-processing-and-returning-a-response/2715>\
**Category:** Questions\
**Created:** [October 4, 2018, 11:21am UTC](https://discourse.slimframework.com/t/exiting-middleware-processing-and-returning-a-response/2715 "2018-10-04T11:21:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheLobos](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@TheLobos](https://discourse.slimframework.com/u/TheLobos)\
**Post date:** [October 4, 2018, 11:21am UTC](https://discourse.slimframework.com/t/exiting-middleware-processing-and-returning-a-response/2715/1 "2018-10-04T11:21:06Z")

</div>

Lets say I have 3 middlewares which are processed for each request. Is it somehow possible to abort further processing of middles in (for example) Middleware #2 and send a response to the requesting browser? How would this be achieved?

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [October 4, 2018, 12:04pm UTC](https://discourse.slimframework.com/t/exiting-middleware-processing-and-returning-a-response/2715/2 "2018-10-04T12:04:57Z")

</div>

You can conditionally return a response before you call the next middleware. Or just don’t call `next` and return the response if you know no further processing is required.

```php
    public function __invoke(Request $request, Response $response, $next)
    {
        if (!$user) {
            return $response; // short circuit the rest of the middleware
        }

        $response = $next($request, $response);

        return $response;
    }

```

---

<div class="post-metadata">

**Author:** ![TheLobos](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@TheLobos](https://discourse.slimframework.com/u/TheLobos)\
**Post date:** [October 4, 2018, 7:05pm UTC](https://discourse.slimframework.com/t/exiting-middleware-processing-and-returning-a-response/2715/3 "2018-10-04T19:05:54Z")

</div>

Great, thank you, will try this tonight …

Edit: works like a charm, thanks a lot!

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [October 4, 2018, 11:11pm UTC](https://discourse.slimframework.com/t/exiting-middleware-processing-and-returning-a-response/2715/4 "2018-10-04T23:11:16Z")

</div>

Great! Middleware can be strange to wrap your head around sometimes. I know it is for me.
