Masoud Motallebipour
Masoud Motallebipour

Reputation: 414

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.

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 :

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

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

Upvotes: 0

Views: 1943

Answers (2)

Ahmed Siouani
Ahmed Siouani

Reputation: 13891

The get() and the getRequest() helpers you're trying to use here are part of the Symfony2 base Controller.

They should be used only in a Controller context once your controller extends the showed above base 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();

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

And any other service for 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,

  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.

Upvotes: 2

Markus Kottländer
Markus Kottländer

Reputation: 8268

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'); inside your service.

Upvotes: 2

Related Questions