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

Using urlFor in route middleware

pudding-en's Avatar

pudding-en

24 May, 2012 04:44 PM

First, let me thank you for your great Framework.

Easy to use and really great to code mini-sites.

I'm starting using PHP and i was wondering if it's possible to use the urlFor in a route middleware. I can't figure it out.

Sorry for the beginner's question and excuse my rusty english.

Thx for your help.

  1. Support Staff 1 Posted by Brian Nesbitt on 18 Jun, 2012 09:20 PM

    Brian Nesbitt's Avatar

    Yes you can. My guess is you are not including the "use($app)" statement as part of the middleware.

    $routeMiddleware = function() use ($app) {
       var_dump($app->urlFor('testingurlfor', array('id' => 'setinroutemiddleware')));
    };
    
    $app->get('/testingurlfor/:id', $routeMiddleware, function ($id) {
       echo "Got Id=".$id;
    })->name('testingurlfor');
    

    You can also access the router via the route itself so you could do something like this as well for the middleware:

    $routeMiddleware = function($req, $res, $route) {
       var_dump($route->getRouter()->urlFor('testingurlfor', array('id' => 'setinroutemiddleware')));
    };
    

    Both give you:

    string(35) "/testingurlfor/setinroutemiddleware"
    Got Id=id
    
  2. Brian Nesbitt closed this discussion on 29 Jun, 2012 01:08 PM.

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