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

Access included files variables from inside a route

MarcosBL's Avatar


08 Feb, 2012 05:53 PM

Any idea how i can access my previously declared variables from inside an included file in a route ?

If i "include()" a file where i set some variables, that i need inside the routed files... variables seems to not be declared.

The idea is i have an old app coded, that i can't modify, and i am using Slim framework to develop an Admin area for it. Everything works fine but that, and i can't touch the old code... ideas ?

Sorry for the TERRIBLE explanation, but english is not my native language, i have set up an example at:

Thanks in advance

  1. Support Staff 1 Posted by Josh Lockhart on 08 Feb, 2012 05:59 PM

    Josh Lockhart's Avatar

    This is a scope issue. You'll either need to use global $var; inside of a route callback (not recommended but it'll work) or pass the variables into the function scope with use like this:

    $foo = 'bar'; //defined in global scope in your included file
    $app->get('/hello', function () use ($app, $foo) {
       echo $foo; //outputs "bar"

    If you'll have a LOT of variables floating around, you're better off moving the variables into 1) a class as static public variables or 2) an array or 3) an instance of stdClass. Then you only need to pass in the class/array/stdClass instance with use rather than each variable.

    Make sense?

  2. Support Staff 2 Posted by Josh Lockhart on 08 Feb, 2012 06:01 PM

    Josh Lockhart's Avatar

    If you cannot touch the old code, you may want to include the old config, collect the necessary vars into a single data structure, and pass that data structure into your routes.

    If you include the remote file within the current file's function scope, the variables should be introduced into the current scope; but I'm not 100% sure on that -- should be easy enough to test quickly though.

  3. 3 Posted by MarcosBL on 08 Feb, 2012 06:26 PM

    MarcosBL's Avatar

    OMG ! That was fast !

    Of course it makes sense, my problem is the "old static code i can't touch", i'm trying right now the second option, including from inside the file... but it doesn't work either.

    Funny part is i can access the functions in the old file without any problem, as well as the DEFINE() variables in the old file... is just the variables that doesn't work... but there are maybe 2 hundreds of them... not even a single class, just $this and $that everywhere.

    Thank you very much for taking a look, will keep trying and keep this updated

  4. 4 Posted by MarcosBL on 09 Feb, 2012 10:31 AM

    MarcosBL's Avatar

    Just as note for others, as i was not able to get it working in any other way, i finally "solved" it via brute-force:

    $excludeList = array('GLOBALS', '_FILES', '_COOKIE', '_POST', '_GET', 'excludeList', 'action', 'app', 'log');
    $temp = array_values(array_diff(array_keys(get_defined_vars()), $excludeList));
    while (list($key, $value) = each($temp)) {
        global $$value;
  5. Support Staff 5 Posted by Josh Lockhart on 09 Feb, 2012 01:37 PM

    Josh Lockhart's Avatar
    $excludeList = array('GLOBALS', '_FILES', '_COOKIE', '_POST', '_GET', 'excludeList', 'action', 'app', 'log');
    $temp = array_values(array_diff(array_keys(get_defined_vars()), $excludeList));
    $app->get('/foo', function () use ($temp) {
        //Use $temp

    Does this not work for you? It would save you from repeating your code in every route callback.

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