简体   繁体   English

getUser()返回null,在我的symfony控制器中没有启动会话

[英]getUser() returns null, session is not started in my symfony controller

On the latest Symfony 2 version (using FOSUserBundle and SonataUserBundle), I am trying to get the current user in order to pre-fill a form in my new Entity controller. 在最新的Symfony 2版本(使用FOSUserBundle和SonataUserBundle)上,我试图让当前用户在我的新实体控制器中预填充表单。

On mycontroller.yml : mycontroller.yml上

my_controller_new:
    pattern:  /new
    defaults: { _controller: "MySiteBundle:MyController:new" }

On MyController.php : MyController.php上

public function newAction()
{
  $user = $this->getUser();
  // ...
}

But when I call $this->getUser() , the method returns null . 但是当我调用$this->getUser() ,该方法返回null Even though I am logged in (I can access /profile and update my user info without any problem) 即使我已登录(我可以访问/profile并更新我的用户信息,没有任何问题)

I then tried to check is the session was started: 然后我试着检查会话是否已启动:

public function newAction()
{
  var_dump($this->get('session')->isStarted());
  // ...
}

It returns false. 它返回false。 What is wrong here? 这有什么不对?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM