8
votes

Symfony doc says:

During each request, Symfony2 will set a global template variable app in both Twig and PHP template engines by default. The app variable is a GlobalVariables instance which will give you access to some application specific variables automatically:
app.security - The security context.
app.user - The current user object.
app.request - The request object.
app.session - The session object.
app.environment - The current environment (dev, prod, etc).
app.debug - True if in debug mode. False otherwise.

Examples:
In twig: {{ app.request.method }}
In PHP: echo $app->getRequest()->getMethod() In twig: {{ app.user.username }}
But for the session object:
In twig: {{ app.session.varname }}
In PHP: // I don't know, do you know how to call it?

I've tried: $session = $app->getSession('uid'); but when I try to store it to a database it tells me:

Catchable Fatal Error: Object of class Symfony\Component\HttpFoundation\Session could not be converted to string in C:\wamp\www...

There's a lack of resources when it comes to PHP templates, but in my case I can't switch for some reasons.

The question in other words, what is the equivalent in PHP templating of:
{{ app.session.varname }}?

3

3 Answers

14
votes

In twig: {{ app.session.varname }}

In PHP: echo $app->getSession()->get('uid');

0
votes
$session = $this->get('session');

if ($session->has('varname')) {
    echo $session->varname
} 
0
votes

have you tried to error_log $app->getSession() to see what it returns?

error_log(var_dump($app->getSession(), true));