I had a similar issue, but using Symfony 3. While eluded to in the previous answer, it was a bit tricky to find out how exactly one would use request_stack to achieve the same thing as scope: request.
In this question's case, it would look something like this:
The services.yml config
services: myservice: class: My\MyBundle\MyService arguments: - '@request_stack' - '@router'
And the MyService Class
<?php namespace My\MyBundle; use Symfony\Component\Routing\RequestContext; class MyService { private $requestStack; private $router; public function __construct($requestStack, $router) { $this->requestStack = $requestStack; $this->router = $router; } public doThing() { $context = new RequestContext(); $context->fromRequest($this->requestStack->getCurrentRequest()); $this->router->setContext($context); // of course, the die is an example die($this->router->generate('BackoffUserBundle.Profile.edit')); } }
Note: Accessing RequestStack in the constructor is advised against since it could potentially try to access it before the request is handled by the kernel. So it may return null when trying to fetch the request object from RequestStack.
http://test/app_dev.php, the links will be with app_dev.php. Forhttp://testthey will be without app_dev.php...