GetUser () returns null, the session does not start in my symfony controller

In the latest version of Symfony 2 (using FOSUserBundle and SonataUserBundle), I am trying to get the current user to pre-populate the form in my new Entity controller.

In mycontroller.yml :

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

In MyController.php :

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

But when I call $this->getUser() , the method returns null . Despite the fact that I logged in (I can access /profile and update user information without any problems)

Then I tried to check if the session was started:

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

It returns false. What is wrong here?

+5
source share
1 answer
 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 } 
0
source

Source: https://habr.com/ru/post/1206086/


All Articles