问题
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:
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
.
.
.
}
来源:https://stackoverflow.com/questions/27226728/how-to-get-session-value-inside-symfony2-i-use-fosuserbundle