The Slim Framework support forum has moved to This Tender forum is no longer maintained or monitored.

Halt Slim application in a Middleware class

jorgeas80's Avatar


01 May, 2012 11:05 AM


How could I stop my application and send a error message to the user inside a Middleware class?

What I've done:

I'm using Slim 1.6.0. I've written my own Middleware class extending Slim_Middleware and implementing call() function. Inside this function, I want to halt the application if some conditions related with header values are not fullfilled. But when I try something like:

$this->app->halt(error_code, error_message);

I get a 500 error caused by Slim.php line 862 (call to Slim->stop). No more information.

Any clues?

Thanks in advance

  1. Support Staff 1 Posted by Josh Lockhart on 01 May, 2012 11:39 AM

    Josh Lockhart's Avatar

    Middleware allows you to manipulate the Request and Response before and/or after the Slim application is run. So In middleware, you'll want to access the Request and Response objects directly. If you need to return an error, set the Response object's status to 4xx or 5xx and do not call the next middleware.

    Your scenario may be better suited for application hooks... hooks are called inside of a Slim app, so you'll be able to call the Slim instance's methods, like hook or notFound, etc. These methods are not suitable for middleware where you are more working with lower level request and response objects.

  2. 2 Posted by jorgeas80 on 01 May, 2012 11:44 AM

    jorgeas80's Avatar

    Mmmm... I think you're right about the hooks. I want to check if a given value is stored in a header's field. If that field is empty, I want to send the user an error. Is it suitable for hooks better than Middleware?

  3. Support Staff 3 Posted by Josh Lockhart on 01 May, 2012 11:52 AM

    Josh Lockhart's Avatar

    Could be done in either just as well. Just be aware of the context for the one you choose.

  4. 4 Posted by jorgeas80 on 01 May, 2012 04:58 PM

    jorgeas80's Avatar

    Basically, I want to check if the Authorization field of the request header is empty, to avoid unauthenticated requests. My approach was using Middleware.

  5. 5 Posted by kishore on 01 May, 2012 05:29 PM

    kishore's Avatar

    I am developing API. Every request needs to be first validated before dispatched to the route. I am using pre dispatch hook. and it just works fine for me. But I am unable to halt the application by calling halt() per my another post.

    Josh: Can't we call halt() from the hooks?

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:


Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac