Invalid request_stack service

I am writing a Symfony 2.6 application and I encountered a problem while trying to inject RequestStack into the service. I want to get the current request from my service, but I get the following exception:

ServiceNotFoundException: The service "host_service" has a dependency on a non-existent service "request_stack". 

My service My code for the service is as follows:

 <?php namespace MyBundle\DependencyInjection; use Symfony\Component\HttpFoundation\RequestStack; class HostService { protected $request; public function __construct(RequestStack $requestStack) { $this->requestStack = $requestStack; } public function getSomething() { $request = $this->requestStack->getCurrentRequest(); // Do stuff with the request stack // return something } } 

Services.yml

I created the services.yml file as follows:

 # MyBundle/Resources/config/services.yml services: host_service: class: MyBundle\DependencyInjection\HostService arguments: ["@request_stack"] 

Extension

I also created an extension for my package:

 <?php namespace MyBundle\DependencyInjection; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\Config\FileLocator; use Symfony\Component\HttpKernel\DependencyInjection\Extension; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; class MyExtension extends Extension { public function load(array $configs, ContainerBuilder $container) { $loader = new YamlFileLoader( $container, new FileLocator(__DIR__.'/../Resources/config') ); $loader->load('services.yml'); } } 

controller

The way I use is as follows:

 $hostService = $this->get('host_service'); $something = $hostService->getSomething(); 

Also tried

I also tried pasting it using a method like this:

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

But that didn't work either (same exception, and yes, I also changed the service in Services.yml).

Question

As per this article on Symfony.com, you should not introduce a request service and use RequestStack instead. I tried to do this in the same material as in the article, but it still won't work.

Did I forget something? I don’t use services at all (this is my first service)? Am I not seeing something? Why does the request_stack service not exist? Most of all: why doesn't it work?

Information that led to the decision

I am using Symfony 2.3 and not Symfony 2.6.

+5
source share
2 answers

Use php app / console container: debug or debug: container for 2.6 to check if the service exists and maybe you are using the wrong version of symfony

+1
source

Since 2017, Symfony 3.3+ is very simple.

1. Register services using autowiring through Auto-create PSR-4 service

 # app/config/services.yml services: _defaults: autowire: true App\: resource: ../../src/App 

2. Require RequestStack when servicing through constructor injection

 <?php namespace App; use Symfony\Component\HttpFoundation\RequestStack; final class SomeService { /** * @var AnotherService */ private $requestStack; public function __construct(RequestStack $anotherService) { $this->requestStack = $requestStack; } public function someMethod() { $request = $this->requestStack->getCurrentRequest(); // ... } } 

3. Or in controllers, Request is required as an argument to the action method

 <?php namespace App; use Symfony\Component\HttpFoundation\Request; final class SomeController { public function someAction(Request $request) { $request->...; } } 

Nothing more is required.

Happy coding!

0
source

All Articles