When I try to inject the @request into any of my services, I get this exception:
ScopeWideningInjectionException: Scope Widening Injection detected:
If you can't use RequestStack directly, you could create a factory service that returns the current request using RequestStack.
# services.yml
app.request:
class: Symfony\Component\HttpFoundation\RequestStack
factory: [ @request_stack, getCurrentRequest ]
Then you can access the current request using the app.request
service.
another way to inject currentRequest
directly:
setter injection:
calls:
- ['setRequest', ['@=service("request_stack").getCurrentRequest()']]
or constrauctor injection:
arguments:
$request: '@=service("request_stack").getCurrentRequest()'
As @simshaun states its best practice to place your service in the request scope. This makes the purpose of the service quite clear.
Note that this will make your service unavailable in other scopes such as the command line. However if your service relies upon the request, you should not be using it on the command line anyway (because there is no request available on the command line.