[英]Symfony 3 - Use session variables?
I would like to use session variables for my site.我想为我的网站使用会话变量。
I made :我做了 :
$request = new Request();
$session = $request->getSession();
if ($session == null)
{
$session = new Session();
}
$session->set('typeAuth','cas');
But in my controler, when I call this session variable by :但是在我的控制器中,当我通过以下方式调用此会话变量时:
$typeAuth = $_SESSION->get('typeAuth');
I've :我有 :
Notice: Undefined variable: session
注意:未定义变量:会话
And I don't understand why我不明白为什么
You can directly inject the SessionInterface
in your constructor, then use it as property of your service.您可以直接在构造函数中注入
SessionInterface
,然后将其用作服务的属性。
In example :例如:
use Symfony\Component\HttpFoundation\Session\SessionInterface; // <--- use this namespace
class AuthCasService extends AuthAbstract implements AuthInterface {
private $serializer;
private $session;
// Inject the SessionInterface ----------------------------------------------------------V-----------------------V
public function __construct(EntityManagerInterface $em, SerializerInterface $serializer, SessionInterface $session)
{
$this->em = $em;
$this->serializer = $serializer;
$this->session = $session;
}
// Your methods ...
}
and then, in your method, remove $session = $request->getSession();
然后,在您的方法中,删除
$session = $request->getSession();
and use $this->session->set('foo', 'bar');
并使用
$this->session->set('foo', 'bar');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.