php 您如何从 Symfony2 中的服务访问用户会话?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7192916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you access a users session from a service in Symfony2?
提问by Adam Stacey
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:
我有一个服务设置,我将容器传递给它。我可以使用以下方法访问 Doctrine 服务之类的东西:
// 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.
任何帮助将非常感激。
采纳答案by Problematic
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
服务是一个 instanceof 类Symfony\Component\HttpFoundation\Session
,因此您应该能够检索具有该名称的会话:
$session = $this->container->get('session');
After you've done your work on the session, call $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->save();
将所有内容写回会话。顺便说一下,我的容器转储中还显示了另外两个与会话相关的服务:
session.storage
instanceof Symfony\Component\HttpFoundation\SessionStorage\NativeSessionStorage
session.storage
实例 Symfony\Component\HttpFoundation\SessionStorage\NativeSessionStorage
session_listener
instanceof Symfony\Bundle\FrameworkBundle\EventListener\SessionListener
session_listener
实例 Symfony\Bundle\FrameworkBundle\EventListener\SessionListener
回答by greg
If you don't want to pass in the entire container you simply pass in @session in the arguments:
如果您不想传入整个容器,只需在参数中传入@session:
services.yml
服务.yml
my.service:
class: MyApp\Service
arguments: ['@session']
Service.php
服务.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');
}
}
回答by Nerjuz
Try this solution:
试试这个解决方案:
use Symfony\Component\HttpFoundation\Session\Session;
$session = new Session();
$session->get('variable');
回答by Bananaapple
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;
}
}
回答by user3258085
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'));
}
}