php 从 TWIG 模板访问会话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8399389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing session from TWIG template
提问by haynar
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?
我在网上搜索了很多如何$_SESSION
从 TWIG 模板访问全局数组并找到了这个:{{app.session.get('index')}}
,但是当我调用它时,它返回一个空字符串。我有一个,$_SESSION['filter']['accounts']
并且在调用{{app.session.get('filter').accounts}}
:时收到此错误Item "accounts" for "" does not exist
。我做错了什么?
回答by Matt
{{app.session}}
refers to the Session
object and not the $_SESSION
array. I don't think the $_SESSION
array is accessible unless you explicitly pass it to every Twig template or if you do an extension that makes it available.
{{app.session}}
指的是Session
对象而不是$_SESSION
数组。我不认为$_SESSION
数组是可访问的,除非你明确地将它传递给每个 Twig 模板,或者你做了一个使它可用的扩展。
Symfony2 is object-oriented, so you should use the Session
object to set session attributes and not rely on the array. The Session
object 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.
Symfony2 是面向对象的,所以你应该使用Session
对象来设置会话属性,而不是依赖数组。该Session
对象会将这些东西从您那里抽象出来,因此更容易将会话存储在数据库中,因为存储会话变量对您是隐藏的。
So, set your attribute in the session and retrieve the value in your twig template by using the Session
object.
因此,在会话中设置您的属性并使用Session
对象检索树枝模板中的值。
// 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.
希望这可以帮助。
Regards,
Matt
问候,
马特
回答by user1279047
Setup twig
设置树枝
$twig = new Twig_Environment(...);
$twig->addGlobal('session', $_SESSION);
Then within your template access session values for example
然后在您的模板访问会话值中,例如
$_SESSION['username'] in php file Will be equivalent to {{ session.username }} in your twig template
回答by Henry
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 :
一个简单的技巧是将 $_SESSION 数组定义为全局变量。为此,通过添加以下函数来编辑扩展文件夹中的 core.php 文件:
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']
回答by joan16v
The way to access a session variable in Twig is:
在 Twig 中访问会话变量的方法是:
{{ app.session.get('name_variable') }}
回答by laurent
I found that the cleanest way to do this is to create a custom TwigExtension and override its getGlobals()
method. Rather than using $_SESSION
, it's also better to use Symfony's Session
class since it handles automatically starting/stopping the session.
我发现最简洁的getGlobals()
方法是创建一个自定义的 TwigExtension 并覆盖它的方法。与其使用$_SESSION
,不如使用 Symfony 的Session
类,因为它会自动处理启动/停止会话。
I've got the following extension in /src/AppBundle/Twig/AppExtension.php:
我在/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 in /app/config/services.yml:
然后在/app/config/services.yml 中添加:
services:
app.twig_extension:
class: AppBundle\Twig\AppExtension
public: false
tags:
- { name: twig.extension }
Then the session can be accessed from any view using:
然后可以使用以下方法从任何视图访问会话:
{{ session.my_variable }}
回答by Somnath De
Why don't you use {{ app.object name.field name}} to access the variable?
为什么不使用 {{ app.object name.field name}} 来访问变量?