![](/img/trans.png)
[英]How to access a symfony session without declaring any variables? (Symfony components interacting with Legacy code use case)
[英]Symfony 3 - Use session variables?
我想为我的网站使用会话变量。
我做了 :
$request = new Request();
$session = $request->getSession();
if ($session == null)
{
$session = new Session();
}
$session->set('typeAuth','cas');
但是在我的控制器中,当我通过以下方式调用此会话变量时:
$typeAuth = $_SESSION->get('typeAuth');
我有 :
注意:未定义变量:会话
我不明白为什么
您可以直接在构造函数中注入SessionInterface
,然后将其用作服务的属性。
例如:
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 ...
}
然后,在您的方法中,删除$session = $request->getSession();
并使用$this->session->set('foo', 'bar');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.