简体   繁体   English

在Symfony的服务类文件中获取会话问题

[英]Getting session issue in Service class files in symfony

I'm using symfony2 and in Service container file I have an issue with working with session. 我正在使用symfony2,并且在Service容器文件中,使用会话存在问题。

this is my service file: 这是我的服务文件:

public function FinalPrice()
{
    $session = $this->get('Currency');
    return  $session;
}

And I have inculuded Response and Session library in the top of the class which is : 我在类的顶部灌入了Response和Session库:

use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

I found it in symfony website which easily working with it without any problem in here 我在symfony网站上找到了它,可以轻松地在这里正常使用它

http://symfony.com/doc/current/book/service_container.html http://symfony.com/doc/current/book/service_container.html

Which says: 其中说:

public function indexAction($bar)
{
    $session = $this->get('session');
    $session->set('foo', $bar);

    // ...
}

but I'm getting this error: 但我收到此错误:

FatalErrorException: Error: Call to undefined method Doobin\CoreBundle\service\GeneralClass::get() in /var/www/doobin92/src/Doobin/CoreBundle/service/GeneralClass.php line 311

I also tried : 我也尝试过:

$session = $this->getRequest()->getSession();

But it give this error: 但是它给出了这个错误:

FatalErrorException: Error: Call to undefined method Doobin\CoreBundle\service\GeneralClass::getRequest() in /var/www/doobin92/src/Doobin/CoreBundle/service/GeneralClass.php line 311

Thank you in adcance 预先感谢您

The get() and the getRequest() helpers you're trying to use here are part of the Symfony2 base Controller. 您在此处尝试使用的get()getRequest()帮助器是Symfony2基本Controller的一部分。

They should be used only in a Controller context once your controller extends the showed above base controller. 一旦您的控制器扩展了上面显示的基本控制器,就应仅在Controller上下文中使用它们。

If you take a look at the code, you may notice that both of them use the container to get the request_stack service for getRequest() ; 如果看一下代码,您可能会注意到他们两个都使用container来获取getRequest()request_stack服务;

$this->container->get('request_stack')->getCurrentRequest();

And any other service for get() , 以及任何其他get()服务,

$this->container->get($id);

As you're trying to call these two methods within a service. 当您尝试在服务中调用这两个方法时。 You're NOT in a Controller Context and the service class you're adding does not provide such helpers. 你在一个控制器上下文不是和您要添加的服务类不提供这样的帮手。

To fix that, 为了解决这个问题,

  1. You've to inject the service you wanted to call through the get() helper directly in your service or, 您必须将要通过get()帮助程序调用的服务直接插入服务中,或者,

  2. Inject the container by adding, 通过添加来注入容器,


use Symfony\Component\DependencyInjection\ContainerInterface;

public YourServiceClass
{
    private $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

to your Service class, 到您的服务等级,

And, 和,

your_service_id:
    class:     YourServiceClass
    arguments: ["@service_container"]

to your service definition. 您的服务定义。

One way would be to inject the service container into your service. 一种方法是将服务容器注入您的服务。

services:
    your_service:
        class:     YourClass
        arguments: ["@service_container"]

And in your service class: 在您的服务类别中:

use Symfony\Component\DependencyInjection\ContainerInterface  as Container;

YourClass {

    protected $container;

    public function __construct(Container $container) {
        $this->container = $container;
    }
}

Then you can use $this->container->get('session'); 然后,您可以使用$this->container->get('session'); inside your service. 在您的服务中。

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

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