Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2: Session Global variable in PHP template

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 }}?

like image 771
Nadjib Mami Avatar asked Apr 11 '12 16:04

Nadjib Mami


1 Answers

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

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

like image 109
Cerad Avatar answered Nov 13 '22 19:11

Cerad