Accessing session from TWIG template

后端 未结 5 556
长发绾君心
长发绾君心 2020-11-29 22:26

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 whe

5条回答
  •  渐次进展
    2020-11-29 22:28

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

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

    Then, you'll be able to acces any session variable as :

    {{ session.username }}
    

    if you want to access to

    $_SESSION['username']
    

提交回复
热议问题