[英]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.