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();
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.