Cannot call set () on frozen ParameterBag

In my config.yml, I have this:

parameters: gitek.centro_por_defecto: 1 

Now I want to change this value from my controller using a form, for example:

 public function seleccionAction(Request $request) { $entity = new Centro(); $form = $this->createForm(new SeleccionType(), $entity); $centro = $this->container->getParameter('gitek.centro_por_defecto'); if ($this->getRequest()->getMethod() == 'POST') { $form->bind($this->getRequest()); if ($form->isValid()) { $miseleccion = $request->request->get('selecciontype'); $this->container->setParameter('gitek.centro_por_defecto', $miseleccion['nombre']); // return $this->redirect($this->generateUrl('admin_centro')); } } return $this->render('BackendBundle:Centro:seleccion.html.twig', array( 'entity' => $entity, 'form' => $form->createView(), )); } 

I get the error Impossible to call set() on a frozen ParameterBag. all the time. Any help or hint?

+5
source share
2 answers

You cannot change the Container after compiling it, which is executed before the controller is called.

DIC parameters are for configuration purposes, and not for replacing global variables. Also, it looks like you want to keep some kind of permanent modification. In this case, consider the possibility of using a session if it is changed for each user or will be stored in it (for example, in the database), if it should be system-wide.

If you need to change the settings or DIC services, you can do this using a compiler skip. More information on how to write custom compiler passes can be found at: http://symfony.com/doc/master/cookbook/service_container/compiler_passes.html

+7
source

You can set $ _ENV variables and get them after

 putenv("VAR=1"); 

And to get

 getenv("VAR"); 
0
source

All Articles