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.
On mycontroller.yml :
my_controller_new:
pattern: /new
defaults: { _controller: "MySiteBundle:MyController:new" }
On MyController.php :
public function newAction()
{
$user = $this->getUser();
// ...
}
But when I call $this->getUser()
, the method returns null
. Even though I am logged in (I can access /profile
and update my user info without any problem)
I then tried to check is the session was started:
public function newAction()
{
var_dump($this->get('session')->isStarted());
// ...
}
It returns 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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.