I am writing a Symfony 2.6 application and I\'ve encountered a problem when trying to inject the RequestStack into a service. What I want is to be able to get the current re
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
Since 2017 and Symfony 3.3+ this is now very simple.
# app/config/services.yml
services:
_defaults:
autowire: true
App\:
resource: ../../src/App
RequestStack
in service via 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();
// ...
}
}
Request
as action method argument<?php
namespace App;
use Symfony\Component\HttpFoundation\Request;
final class SomeController
{
public function someAction(Request $request)
{
$request->...;
}
}
Nothing more is needed.
Happy coding!