本文介绍了如何在symfony2中获取会话值,我使用fosuserbundle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用fosuserbundle,这是我在FOSUBUserProvider类中的函数:

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');
     //
}

推荐答案

您需要在服务声明"中插入会话,

you need to inject Session in your Services Declaration,

,然后将其添加到FOSUserProvider类的构造函数中,

and then add it in constructor of FOSUserProvider class,

中添加@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]

在您的类的connect函数上方

声明$session和$ em变量,并添加以下构造函数,

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);
    }

在功能Connect中,您可以将其保存为

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
     . 
     .
     .
}

这篇关于如何在symfony2中获取会话值,我使用fosuserbundle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 11:12