How can i define global variables in slim framework

Uchenna picture Uchenna · May 14, 2014 · Viewed 14.4k times · Source

How can i define a global variable such that my current_user method can work were ever i want it to, all i simple need to do is check if there is a current user my example code is below

    if (isset($_SESSION['company_id'])) {
       $current_user = Companies::find($_SESSION['company_id']);
     }
    else
   {
    $current_company = null;
   }

how can i use the current user method where ever i want without passing it to my app->render() method just like in my header.html

{% if current_user %}
 hi {{current_user.name}}
{% endif %}

Answer

user2555476 picture user2555476 · May 14, 2014

You can inject a value into the app object:

$app->foo = 'bar';

More on Slim’s documentation.