I need to get a parameter/prefix from the request into a service, and use that service into a twig.
Relevant code from twig:
<a href="{{ path('newsPage', {'edition': edition.edition}) }}">News</a>
Relevant code from twig conf:
twig:
globals:
edition: '@core.edition.service'
Relevant code from service:
class EditionService
{
protected $edition;
// Called function or constructor here - doesn't really matter
public function setEditionFromRequest(Request $request)
{
$this->edition = $request->get('edition');
return $this;
}
public function getEdition()
{
return $this->edition;
}
}
After checking multiple similar issues (like this for ex. How to inject the @request into a service?) I tried the following:
core.edition.service:
class: CoreBundle\Service\EditionService
scope: request
arguments: // Used constructor in service for this instead of the setEditionFromRequest function
- "@request"
This worked ok (I get what I need in service) until it gets to the twig part where I got:
Scope Widening Injection detected: The definition "twig" references the service "core.edition.service" which belongs to a narrower scope. Generally, it is safer to either move "twig" to scope "request" or alternatively rely on the provider pattern by injecting the container itself, and requesting the service "core.edition.service" each time it is needed. In rare, special cases however that might not be necessary, then you can set the reference to strict=false to get rid of this error.
If I remove the scope from the service I get (as expected actually):
Scope Widening Injection detected: The definition "core.edition.service" references the service "request" which belongs to a narrower scope. Generally, it is safer to either move "core.edition.service" to scope "request" or alternatively rely on the provider pattern by injecting the container itself, and requesting the service "request" each time it is needed. In rare, special cases however that might not be necessary, then you can set the reference to strict=false to get rid of this error.
So... I tried the following approach:
core.edition.service:
class: CoreBundle\Service\EditionService
calls:
- [setEditionFromRequest, ["@service_container"]]
And in service:
public function setEditionFromRequest($container)
{
$this->edition = $container->get('request')->get('edition');
return $this;
}
Now everything appeared to work fine except that every few requests (random-ish) I get this (after adding a debug I noticed that the service is instantiated ok, but edition is null - ONLY IN SOME REQUESTS -> if someone can explain how this is possible I would really apreciate it):
An exception has been thrown during the rendering of a template ("Parameter "edition" for route "newsPage" must match "[^/]++" ("" given) to generate a corresponding URL.") in base.html.twig at line 94.
Also if I try to clear cache I get this:
app/console --env=dev cache:clear
Clearing the cache for the dev environment with debug true [Symfony\Component\DependencyInjection\Exception\InactiveScopeException] You cannot create a service ("request") of an inactive scope ("request").
Any idea how can I fix this? For the second problem I think it is because in cli I don't have the request scope.