how to get session value inside symfony2 i use fosuserbundle

ぐ巨炮叔叔 提交于 2019-12-23 02:48:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!