How to inject the @request into a service?

In Symfony 2.4, this has changed. Now, you can inject the ‘request_stack’ service.

For example:

use Symfony\Component\HttpFoundation\RequestStack;

class MyService
{

    protected $request;

    public function setRequest(RequestStack $request_stack)
    {
        $this->request = $request_stack->getCurrentRequest();
    }

}

In your config.yml:

services:
    my.service:
        class: Acme\DemoBundle\MyService
        calls:
            - [setRequest, ["@request_stack"]]

Full documentation is here: http://symfony.com/blog/new-in-symfony-2-4-the-request-stack

Leave a Comment

tech