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

Pass my array to $app

ntan72's Avatar

ntan72

07 Aug, 2012 10:26 AM

Hi i am trying to pass an array that holds error messages to app with the code above

$app = new Slim(array( 'view' => new TwigView, 'my_error_codes' => $CODES ));

where $CODES is the array that holds the messages.

I dont get any error but i dont know how to access the array

Using
$app ->my_error_codes[404]

i get Undefined property: Slim::$my_error_codes

Is there any way that i can implement this.
What am i missing

Thanks

  1. Support Staff 1 Posted by Andrew Smith on 07 Aug, 2012 12:25 PM

    Andrew Smith's Avatar

    You would have to get these through the config method of Slim.

    $app->config('my_error_codes');
    

    If you passing these error codes to the templates only and since you are using Twig from your example you could use Twig addGlobal method.

    $app->view()->getEnvironment()->addGlobal('my_error_codes', $CODES);
    

    I hope this has been helpful.

  2. 2 Posted by ntan72 on 08 Aug, 2012 05:26 AM

    ntan72's Avatar

    I used the config because that suites my needs but i was getting error when trying to get tha var
    $app->config(my_error_codes[404]);

    Even its array you have to get the var using
    $app->config('my_error_codes[404]');

    OR
    $my_error_codes=$app->config('my_error_codes'); echo $my_error_codes[404]

    Its working as expected now.
    Thank for your reply.

  3. Support Staff 3 Posted by Andrew Smith on 08 Aug, 2012 09:20 AM

    Andrew Smith's Avatar

    You would not be able to get an index of the array using the first and second method you provided. Because $app->config is accessing an array using its key, since the key you set was my_error_codes and not my_error_codes[404].

    $my_error_codes = $app->config('my_error_codes');
    echo $my_error_codes[404];
    

    Is the correct way to go as you worked out in the end.

  4. Andrew Smith closed this discussion on 08 Aug, 2012 09:20 AM.

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