I’ve searched a lot on the net how to access the global $_SESSION array from TWIG template and found this: {{app.session.get('index')}}, but when I’m calling it, it returns an empty string. I have a $_SESSION['filter']['accounts'] and I’m getting this error when calling {{app.session.get('filter').accounts}}: Item "accounts" for "" does not exist. What I’m doing wrong?
I’ve searched a lot on the net how to access the global $_SESSION array
Share
{{app.session}}refers to theSessionobject and not the$_SESSIONarray. I don’t think the$_SESSIONarray is accessible unless you explicitly pass it to every Twig template or if you do an extension that makes it available.Symfony2 is object-oriented, so you should use the
Sessionobject to set session attributes and not rely on the array. TheSessionobject will abstract this stuff away from you so it is easier to, say, store the session in a database because storing the session variable is hidden from you.So, set your attribute in the session and retrieve the value in your twig template by using the
Sessionobject.