如何在symfony2内部获得会话值,我使用fosuserbundle[英] how to get session value inside symfony2 i use fosuserbundle

本文是小编为大家收集整理的关于如何在symfony2内部获得会话值,我使用fosuserbundle的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在使用fosuserbundle,这是我在fosubuserprovider类中的功能:

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

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

推荐答案

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

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

in services.yml和服务部分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]

声明$session和$ em变量在上面的类connect函数上,并添加以下构造函数,

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

在功能中Connect您可以将其作为

获得
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://www.itbaoku.cn/post/2090796.html

问题描述

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,

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