title: Symfony - Inject request into a service and use that service in a twig
body: <p>I need to get a parameter/prefix from the request into a service, and use that service into a twig.</p>
  
  <p>Relevant code from twig:</p>
  
  <pre><code>&lt;a href="{{ path('newsPage', {'edition': edition.edition}) }}"&gt;News&lt;/a&gt;
  </code></pre>
  
  <p>Relevant code from twig conf:</p>
  
  <pre><code>twig:   
      globals:
         edition: '@core.edition.service'
  </code></pre>
  
  <p>Relevant code from service:</p>
  
  <pre><code>class EditionService
  {
     protected $edition;
  
     // Called function or constructor here - doesn't really matter
     public function setEditionFromRequest(Request $request)
     {
        $this-&gt;edition = $request-&gt;get('edition');
  
        return $this;
     }
  
  
     public function getEdition()
     {
        return $this-&gt;edition;
     }
  }
  </code></pre>
  
  <p>After checking multiple similar issues (like this for ex. <a href="https://stackoverflow.com/questions/9332925/how-to-inject-the-request-into-a-service">How to inject the @request into a service?</a>) I tried the following:</p>
  
  <pre><code>core.edition.service: 
      class: CoreBundle\Service\EditionService
      scope: request
      arguments:    // Used constructor in service for this instead of the setEditionFromRequest function
          - "@request"
  </code></pre>
  
  <p>This worked ok (I get what I need in service) until it gets to the twig part where I got:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>If I remove the scope from the service I get (as expected actually):</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>So... I tried the following approach:</p>
  
  <pre><code>core.edition.service:
    class: CoreBundle\Service\EditionService
    calls:
        - [setEditionFromRequest, ["@service_container"]]
  </code></pre>
  
  <p>And in service:</p>
  
  <pre><code>public function setEditionFromRequest($container)
  {
      $this-&gt;edition = $container-&gt;get('request')-&gt;get('edition');
  
      return $this;
  }
  </code></pre>
  
  <p>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 - <strong>ONLY IN SOME REQUESTS -> if someone can explain how this is possible I would really apreciate it</strong>):</p>
  
  <blockquote>
    <p>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. </p>
  </blockquote>
  
  <p>Also if I try to clear cache I get this:</p>
  
  <pre><code>app/console --env=dev cache:clear
  </code></pre>
  
  <blockquote>
    <p>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").</p>
  </blockquote>
  
  <p>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.</p>