简体   繁体   English

如何从Symfony2中的服务访问用户会话?

[英]How do you access a users session from a service in Symfony2?

Does anyone know how to access the session variable in a service? 有谁知道如何访问服务中的会话变量?

I have a service setup that I pass the container over to. 我有一个将容器传递给的服务设置。 I can access things like the Doctrine service by using: 我可以使用以下方法来访问“教义”服务:

// Get the doctrine service
$doctrine_service = $this->container->get('doctrine');  
// Get the entity manager
$em = $doctrine_service->getEntityManager();

However, I'm not sure how to access the session. 但是,我不确定如何访问该会话。

In a controller the session can be accessed using: 在控制器中,可以使用以下命令访问会话:

$session = $this->getRequest()->getSession();
$session->set('foo', 'bar');
$foo = $session->get('foo');

Is the session part of a service and if so what is the service called. 会话是服务的一部分,如果是的话,该服务称为什么。

Any help would be really appreciated. 任何帮助将非常感激。

If you don't want to pass in the entire container you simply pass in @session in the arguments: 如果您不想传递整个容器,则只需在参数中传递@session即可:

services.yml services.yml

my.service:
    class: MyApp\Service
    arguments: ['@session']

Service.php Service.php

use Symfony\Component\HttpFoundation\Session\Session;

class Service
{

    private $session;

    public function __construct(Session $session)
    {
        $this->session = $session;
    }

    public function someService()
    {
        $sessionVar = $this->session->get('session_var');
    }

}

php app/console container:debug shows that the session service is a class instanceof Symfony\\Component\\HttpFoundation\\Session , so you should be able to retrieve the session with that name: php app/console container:debug显示session服务是Symfony\\Component\\HttpFoundation\\Session的类实例,因此您应该能够使用该名称检索会话:

$session = $this->container->get('session');

After you've done your work on the session, call $session->save(); 完成会话工作后,调用$session->save(); to write everything back to the session. 将所有内容写回会话。 Incidentally, there are two other session-related services that show in my container dump, as well: 顺便说一句,我的容器转储中还显示了其他两个与会话相关的服务:

session.storage instanceof Symfony\\Component\\HttpFoundation\\SessionStorage\\NativeSessionStorage Symfony\\Component\\HttpFoundation\\SessionStorage\\NativeSessionStorage session.storage实例

session_listener instanceof Symfony\\Bundle\\FrameworkBundle\\EventListener\\SessionListener session_listener实例Symfony\\Bundle\\FrameworkBundle\\EventListener\\SessionListener

Try this solution: 试试这个解决方案:

use Symfony\Component\HttpFoundation\Session\Session;

$session = new Session();
$session->get('variable');

In Symfony 4, if you have your services configured to autowire you do not need to inject the session manually through services.yaml any longer. 在Symfony 4中,如果您将服务配置为自动连线,则不再需要通过services.yaml手动注入会话。

Instead you would simply inject the session interface: 相反,您只需注入会话接口即可:

use Symfony\Component\HttpFoundation\Session\SessionInterface;

class SessionService
{
    private $session;

    public function __construct(
        SessionInterface $session
    )
    {
        $this->session = $session;
    }
}

faz assim que vai funcionar Faz Assim que Vai Funcionar

use Symfony\Component\HttpFoundation\Session\Session;

class ClassType extends AbstractType {
    private $session;

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options) {

        $this->session = new Session();

    ->add('field', $this->session->get('nameFieldSession'));

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM