title: How can I dynamically set a parameter in Symfony2?
body: <p>I'm trying to dynamically set a parameter in Symfony2 (that I cannot statically set in my <code>parameters.yml</code> file). My approach is to use an <code>EventListener</code>:</p>
  
  <pre><code>namespace Acme\AcmeBundle\EventListener;
  
  use Symfony\Component\DependencyInjection\Container;
  
  class AcmeListener
  {
      private $container;
  
      public function __construct(Container $container)
      {
          $this-&gt;container = $container;
      }
  
      public function onKernelRequest()
      {
          // Dynamically fetch $bar
          $bar = fetch('foobar');
  
          // Set parameter
          $this-&gt;container-&gt;setParameter('foo', $bar);
      }
  }
  </code></pre>
  
  <p>And my service definition in <code>config.yml</code> looks like this:</p>
  
  <pre><code>service:
      kernel.listener.acme_listener:
          class: Acme\AcmeBundle\EventListener\AcmeListener
          tags:
              - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
          arguments: [ '@service_container' ]
  </code></pre>
  
  <p>The problem is, I get an exception:</p>
  
  <blockquote>
    <p>LogicException: Impossible to call set() on a frozen ParameterBag.</p>
  </blockquote>
  
  <p>How can I work around this exception or do you see another way to dynamically set a parameter?</p>