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

Hooks and Variables

jeremytiki's Avatar


11 Jul, 2012 07:06 PM

Ok so what Im trying to do is get a variable from my route function into the 'slim.after.route' hook, but Im not sure how. The documentation says "Hooks do not pass arguments to their callables. If a callable needs to access the Slim application, you can inject the application into the callback with the use keyword or with the static getInstance() method:" and that's what I've tried doing, making my hook "use($app)" but I'm still not quite sure how to access the variable since it is defined inside the actual route function.

  1. Support Staff 1 Posted by Andrew Smith on 11 Jul, 2012 07:18 PM

    Andrew Smith's Avatar

    Could you state what version of Slim you are using and post an example of

  2. 2 Posted by jeremytiki on 11 Jul, 2012 07:21 PM

    jeremytiki's Avatar

    The most recent stable version
    And my code:
    1 <?php 2 $app->get('/', function () { 3 $package = ""; 4 }); settings.php
    1 <?php 2 3 $app->hook('slim.after.router', function () use ($app) { 4 echo "

      5                 print_r($package);
      6         });
    What I'm wanting to do is have access to the string inside of the home page route via $package.
  3. 3 Posted by jeremytiki on 11 Jul, 2012 07:27 PM

    jeremytiki's Avatar

    Sorry about that,

    $app->get('/', function () { 
        $package = ""; 


    $app->hook('slim.after.router', function () use ($app) {
        echo "<pre>";

    What I'm wanting to do is have access to the string inside of the home page route via $package.

  4. Support Staff 4 Posted by Andrew Smith on 11 Jul, 2012 07:27 PM

    Andrew Smith's Avatar

    That would not be possible with the current setup since $package is inside
    an annonymous function, if you were to pass package to Skin's environment
    variable then you could access it through $app.

  5. 5 Posted by jeremytiki on 11 Jul, 2012 07:29 PM

    jeremytiki's Avatar

    What would be the best way to do that?

  6. Support Staff 6 Posted by Brian Nesbitt on 11 Jul, 2012 09:41 PM

    Brian Nesbitt's Avatar

    In the route you could do the following:

    $env = $app->environment();
    $env['package'] = "";

    Then in the slim.after.router hook you can access the variable like:

    $env = $app->environment();

    The only thing I don't like about this is the package string is used in two places. It works but could be improved. If this is something that you will be using in several other places (ie every route) then you could extend the Slim class and add a nice helper function:

    class PackageSlim extends Slim {
        public function setPackage($name) {
            $env = $this->environment();
            $env['package'] = $name;
        public function getPackage() {
            $env = $this->environment();
            return $env['package'];

    At least this centralized the access. Now you just do $app = new PackageSlim();.

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


? 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