我在控制器中保存會話中的值;無法獲取會話變量值symfony
public function setSessionValue($key,$val){
$session = $this->get('session');
$session->set($key,$val);
echo "Token is: ".$session->get('token');
}
獲得答案爲;
令牌是:e129cb3b547b6017a9f69fe30a96efed
在另一個請求我從會話使用另一種方法獲取該標記值;
public function getSessionValue($key){
$session = $this->get('session');
echo "Token is: ".$session->get('token');
return $session->get($key);
}
現在的反應是:
令牌是:NULL
我security.yml如下文件;
# To get started with security, check out the documentation:
# https://symfony.com/doc/current/security.html
security:
# https://symfony.com/doc/current/security.html#b-configuring-how-users-are-loaded
providers:
in_memory:
memory: ~
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: ~
# activate different ways to authenticate
# https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
#http_basic: ~
# https://symfony.com/doc/current/security/form_login_setup.html
#form_login: ~
我的全控制器類是:
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Session\Session;
class BaseController extends Controller{
public $request;
public function setSessionValue($key,$val){
$session = $this->get('session');
$session->set($key,$val);
echo "Token is: ".$session->get('token');
}
public function getSessionValue($key){
$session = $this->get('session');
echo "Token is: ".$session->get('token');
return $session->get($key);
}
}
然後我做這樣的事情;
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Doctrine\ORM\EntityManagerInterface;
use AppBundle\Entity\UserData;
use AppBundle\Bl\LoginOperation;
use AppBundle\Controller\BaseController;
class LoginController extends BaseController
{
/**
* @Route("/login", name="loginpage")
*/
public function loginAction($request)
{
/* more code */
//storing token
$this->setSessionValue('token',$obj->token);
$this->setSessionValue('uid',$user->getUserId());
$response = json_encode($obj);
return new Response($response);
}
}
可能是什麼原因呢?我嘗試了各種方式,但無法檢索第二個請求中的值。
任何想法對我都很有幫助。
什麼是「這個」控制器? –
是的,這是一個控制器。 –
您的會話是如何創建的?它是Symfony Session還是本地PHP會話? 爲什麼你不使用tokenStorage? – Hornth