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

Route Helpers in Middleware

Tomas's Avatar

Tomas

10 Jul, 2012 08:49 AM

Hi,

Is it possible to use route helpers like halt(), stop(), redirect() inside middlewares?

Currently I'm implementing authentication on some routes as follows:

$app = new Slim();
$authenticateFunction = function ( $app ) { 
    $user = ...; $pwd = .... 
    $auth = new CustomAuthClass($user, $pwd)
    if (!$auth->authenticate()) {
           // if not allowed, stop.
           $app->stop(); // I can't do this. 
    }
};

$app->get("/users/:user_id", $authenticateFunction($app), function () use ($app) {
    echo 'private stuff';
});

But this is failing due to an uncaught exception, since the Slim_Exception_Stop is not handled.

Ideas?

BTW the authentication example in docs doesn't work (I'm running PHP 5.3.5). $authenticateForRole is not defined correctly, also shouldn't Slim::flash and Slim:redictect be defined as static in Slim.php?

$authenticateForRole = function ( $role = 'member' ) {
    return function () use ( $role ) {
        $user = User::fetchFromDatabaseSomehow();
        if ( $user->belongsToRole($role) === false ) {
            Slim::flash('error', 'Login required');
            Slim::redirect('/login');
        }
    }
}
$app = new Slim();
$app->get('/foo', $authenticateForRole('admin'), function () {
    //Display admin control panel
});

Thanks,
Tomas

  1. Support Staff 1 Posted by Brian Nesbitt on 10 Jul, 2012 01:08 PM

    Brian Nesbitt's Avatar

    That is happening because you are not passing in a callable parameter. Instead you are actually executing the function and passing in the result which in your case is nothing since it doesn't return anything and just throws the exception which of course isn't caught by anything since $app->run() hasn't been called yet and hasn't setup the exception handler.

    You can easily fix it by wrapping it in a return function statement. Also you'll see below that the middleware is passed the request, response, route instances as parameters so you can't pass in any other parameters, but you can use the $app as illustrated.

    $app = new Slim();
    
    $authenticateFunction = function ($app) {
        return function($req, $res, $route) use ($app) {
            // commented out so I could run it            
            //$user = ...; $pwd = .... 
            //$auth = new CustomAuthClass($user, $pwd)
            if (true) {
               // if not allowed, stop.
               $app->stop(); // I CAN now do this !!
            }
        };
    };
    
    $app->get("/users/:user_id", $authenticateFunction($app), function () {
        echo 'private stuff';
    });
    

    And yes, thanks, the documented code sample won't work as is. I assume the intent was to use the static getter and do Slim::getInstance()->flash('error', 'Login required'); and it was just missed. I get that corrected.

  2. 2 Posted by Tomas on 10 Jul, 2012 04:38 PM

    Tomas's Avatar

    Awesome!

    Now I see the point of the function return, thanks a lot for the quick reply!

  3. Brian Nesbitt closed this discussion on 12 Jul, 2012 03:58 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