[英]$this->getUser() in Symfony controller functional test returns null
[英]getUser() returns null, session is not started in my symfony controller
在最新的Symfony 2版本(使用FOSUserBundle和SonataUserBundle)上,我试图让当前用户在我的新实体控制器中预填充表单。
在mycontroller.yml上 :
my_controller_new:
pattern: /new
defaults: { _controller: "MySiteBundle:MyController:new" }
在MyController.php上 :
public function newAction()
{
$user = $this->getUser();
// ...
}
但是当我调用$this->getUser()
,该方法返回null
。 即使我已登录(我可以访问/profile
并更新我的用户信息,没有任何问题)
然后我试着检查会话是否已启动:
public function newAction()
{
var_dump($this->get('session')->isStarted());
// ...
}
它返回false。 这有什么不对?
public function newAction() {
$session = $this->getRequest()->getSession(); // Get started session
if(!$session instanceof Session)
$session = new Session(); // if there is no session, start it
$value = $session->getId(); // get session id
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.