how to get session value inside symfony2 i use fos

2019-06-05 02:27发布

i'm using fosuserbundle and this is my function inside FOSUBUserProvider class :

public function connect(UserInterface $user, UserResponseInterface $response)
    {

      // and here i want to get session value like:
       $session = $request->getSession();
       $session->get('value1');
     //
}

1条回答
▲ chillily
2楼-- · 2019-06-05 03:07

you need to inject Session in your Services Declaration,

and then add it in constructor of FOSUserProvider class,

in services.yml and services section add @session

parameters:
    my_user_provider.class: Auth\UserBundle\Security\Core\User\FOSUBUserProvider

services:
    my_user_provider:
        class: "%my_user_provider.class%"
        #this is the place where the properties are passed to the UserProvider class
        arguments: [@fos_user.user_manager,{facebook: facebookID},@session,@doctrine.orm.entity_manager]

declare $session and $em variable in your class above connect function and add following constructor,

public function __construct(UserManager $userManager, Array $properties, Session $session, EntityManager $em)
    {
        $this->session=$session;
        $this->em=$em;
        parent::__construct($userManager, $properties);
    }

in function Connect you can get it as,

public function connect(UserInterface $user, UserResponseInterface $response)
    {
       $value=$this->session->get('value1');
       $em=$this->em;  // or directly use $this->em->flush(); or whatever you want
     . 
     .
     .
}
查看更多
登录 后发表回答