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

Route Middleware Parameters

Marc's Avatar

Marc

21 Oct, 2014 07:26 AM

Hi Guys,

I'm having a bit of a brain ache getting this to work:

$app->get('/api/getlist/:key' , $middleware($key), function($key) use($db){})

The $key variable is always empty from within the middleware declared like so

$middleware = function($key) use ($app, $db) {
                return function() use($app, $db, $key){
                      var_dump($key); // string 0
               };
   };

Any help greatly appreciated!

Cheers

Marc

  1. 1 Posted by Marc Fielding on 21 Oct, 2014 07:56 PM

    Marc Fielding's Avatar

    Anyone?

  2. 2 Posted by Marc Fielding on 22 Oct, 2014 06:43 AM

    Marc Fielding's Avatar

    Ok after a bit more reading it appears that you can't pass route parameters to middleware - the only thing that gets passed if you specify a variable as a parameter is the route object.

    Unfortunately the route parameters are a protected property.

    Easiest way is to have a function call like normal at the top of your route code for things like authorisation at this stage unless i'm missing something?

    Cheers

    Marc

  3. 3 Posted by dsquier on 05 Aug, 2015 11:52 PM

    dsquier's Avatar

    Thanks to @silentworks, this is how I did it:

    Assuming a route:

    $app->get('/api/getlist/:key' , $middleware($key), function() {} );
    

    Then, to use key in $middleware, do this:

    var key = $app->router->getCurrentRoute()->getParam('key')
    

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

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