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

URI from a hook

kishore's Avatar


01 Aug, 2012 06:45 PM

I have slim.before hook that validates all calls before allowing to go to the actual route. But I also need to validate part of URI in every call. Is it possible to get the URI in the hook function or is it possible to get it as a parameter (/Events/:guid/test/)?

  1. 1 Posted by kishore on 01 Aug, 2012 07:06 PM

    kishore's Avatar

    Well, I could get the URI from $app object. Is it possible to get it as a parameter? (2nd part of the question).

  2. Support Staff 2 Posted by Brian Nesbitt on 02 Aug, 2012 03:43 AM

    Brian Nesbitt's Avatar

    Yes the URI is $app->request()->getPathInfo() as you probably saw.

    At the time the slim.before hook is called the route params have not been parsed yet so you wouldn't be able to access guid in your example above at that time. It does look like though if you called $app->router()->getCurrentRoute() it will run the route matching and cache it for later use so you could actually do it.

    However, the slim.before.dispatch hook seems to match your need better and you can access the route parameters like this:

    $app->hook('slim.before.dispatch', function () use ($app) {
        $route = $app->router()->getCurrentRoute();
        // Provide a better validation here... up to you!
        if ($route->getParam('guid') !== "59F1D46E-DC52-11E1-A9DD-B6EE6188709B") {
            $app->halt(403, "Stop the aliens!");
    $app->get('/events/:guid/list/', function ($guid) {
        printf("You are a real human user with a cool guid of %s.", $guid);

    This will check all routes for a valid guid before allowing it to be processed. If you don't want to check all routes (like you have some public routes or not all routes have a guid and thus you get the exception) you could:
    -check the request uri and ensure it starts with a certain directory -use the $route->getParams() and check for array key exits / isset() to avoid the exception -change the hook to be a route middleware and only apply it to routes that need verification -apply a name with a particular string to match and check that in the hook to see if it requires verification.

  3. 3 Posted by kishore on 03 Aug, 2012 07:22 PM

    kishore's Avatar

    Thanks Josh! This is helpful.
    I actually ended up parsing the Path to get the value I need.
    But this looks good.

  4. Support Staff 4 Posted by Brian Nesbitt on 03 Aug, 2012 07:36 PM

    Brian Nesbitt's Avatar

    My name is Brian, but your welcome anyway :-)

  5. Brian Nesbitt closed this discussion on 03 Aug, 2012 07:36 PM.

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

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