The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Slim 1.6.0 - Middleware

Mirko's Avatar

Mirko

26 Mar, 2012 01:56 PM

Hi everyone.

I am developing on 1.6.0 develop branch, and I need to manage HTTP Authentication.

In 1.5.x my idea of managing HTTP Authentication was to use Middleware, so I am able to request the Authentication only on the routes I need it, leaving the other routes free from Authentication.

Now, I am facing the problem, but 1.6.0 has another concept of Middleware, and here I have to implement the Rack Protocol, creating my own middleware classes which implements the Slim_Middleware_Interface.

My doubt is simple... If I have to add those middlewares to the $app, how can I add middlewares based on routes?

Am I wrong or the previous way (1.5.x) is more effective?

Thanks

Mirko

  1. Support Staff 1 Posted by Andrew Smith on 26 Mar, 2012 03:39 PM

    Andrew Smith's Avatar

    I personally haven't used the new Middleware as yet, but looking at the examples that Josh has in the docs you should be able to check the routes from inside the middleware itself since you will instantiate $app in the constructor.

  2. Support Staff 2 Posted by Josh Lockhart on 26 Mar, 2012 05:10 PM

    Josh Lockhart's Avatar

    Hi Mirko,

    As Andrew said, there are several examples in the development branch documentation:

    http://www.slimframework.com/documentation/develop#middleware

    Before 1.6.0 is officially released, I do plan on tweaking how application middleware works so that its interface is simpler and easier to understand. That's the last thing I have left to complete before the 1.6.0 release.

    There is also route middleware already available in 1.5.x... this is what has and will continue to exist in the Slim Framework. Route middleware can be specified per route and will be invoked in sequence before the given route's callable is invoked. Route middleware looks like this:

    $app = new Slim();
    $app->get('/foo', 'routeMiddleware', function () {
        //Route callable
    });
    function routeMiddleware() {
        echo "I'm middleware!";
    }
    $app->run();
    

    Application middleware (to be available in 1.6.0) will be application-wide and may act on the Request and Response before and after the Slim application itself is run (very much like Rack middleware does).

  3. 3 Posted by Mirko on 24 Apr, 2012 06:48 AM

    Mirko's Avatar

    Great, many thanks guys.
    I have been so busy lately, the information about the future of route middleware are great for me. :)

  4. Support Staff 4 Posted by Josh Lockhart on 24 Apr, 2012 11:42 AM

    Josh Lockhart's Avatar

    Great. Now that version 1.6 is out, you can read about the final implementation here:

    Application-wide middleware:
    http://www.slimframework.com/documentation/stable#middleware

    Route middleware:
    http://www.slimframework.com/documentation/stable#routing-middleware

    Best,
    Josh

  5. Josh Lockhart closed this discussion on 24 Apr, 2012 11:42 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? 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