Session access from TWIG template

I searched a lot on the net how to access the $_SESSION global array from the TWIG template and found this: {{app.session.get('index')}} , but when I call it, it returns an empty string. I have $_SESSION['filter']['accounts'] and I get this error when calling {{app.session.get('filter').accounts}} : Item "accounts" for "" does not exist . What am I doing wrong?

+43
php symfony session twig
Dec 6 2018-11-11T00:
source share
6 answers

{{app.session}} refers to the Session object, not the $_SESSION . I don't think the $_SESSION is available unless you explicitly pass it to each Twig template or if you use the extension that makes it available.

Symfony2 is object oriented, so you should use a Session object to set session attributes and not rely on an array. The Session object will abstract this material from you, so it’s easier, say, to save the session in the database, because the storage of the session variable is hidden from you.

So, set your attribute in the session and retrieve the value in your branch template using the Session object.

 // In a controller $session = $this->get('session'); $session->set('filter', array( 'accounts' => 'value', )); // In Twig {% set filter = app.session.get('filter') %} {% set account-filter = filter['accounts'] %} 

Hope this helps.

Respectfully,
Matt

+114
Dec 06 '11 at 12:52
source share

A simple trick is to define the $ _SESSION array as a global variable. To do this, edit the core.php file in the extension folder, adding this function:

 public function getGlobals() { return array( 'session' => $_SESSION, ) ; } 

Then you can use any session variable as:

 {{ session.username }} 

if you want to access

 $_SESSION['username'] 
+20
Dec 11 '11 at 18:09
source share

Twig Setting

 $twig = new Twig_Environment(...); $twig->addGlobal('session', $_SESSION); 

Then, within your template access values, e.g.

 $_SESSION['username'] in php file Will be equivalent to {{ session.username }} in your twig template 
+10
Feb 17 '16 at 19:20
source share

The way to access the session variable in Twig:

 {{ app.session.get('name_variable') }} 
+6
Dec 01 '16 at 9:12
source share

I found that the cleanest way to do this is to create a custom TwigExtension and override its getGlobals() method. Instead of using $_SESSION also better to use the Symfony Session class, since it automatically starts / stops a session.

I have the following extension in /src/AppBundle/Twig/AppExtension.php :

 <?php namespace AppBundle\Twig; use Symfony\Component\HttpFoundation\Session\Session; class AppExtension extends \Twig_Extension { public function getGlobals() { $session = new Session(); return array( 'session' => $session->all(), ); } public function getName() { return 'app_extension'; } } 

Then add this to /app/config/services.yml :

 services: app.twig_extension: class: AppBundle\Twig\AppExtension public: false tags: - { name: twig.extension } 

Then, access to the session can be obtained from any view using:

 {{ session.my_variable }} 
+5
Aug 27 '15 at 14:48
source share

Why don't you use {{app.object name.field name}} to access the variable?

0
Aug 03 '14 at 9:19
source share



All Articles