9

在我的 config.yml 我有这个:

parameters:
    gitek.centro_por_defecto: 1

现在,我想使用表单从我的控制器更改此值,如下所示:

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(),
    ));
}

Impossible to call set() on a frozen ParameterBag.一直在出错。任何帮助或线索?

4

2 回答 2

8

You can't modify Container once it has been compiled, which is done before invoking the controller.

The DIC parameters are intended for configuration purposes - not a replacement for global variables. In addition it seems you want to persist some kind of permanent modification. In that case consider using session if it's a per-user modification or persisting it (e.g. into DB) if it's supposed to be application-wide.

If you need to modify DIC parameters or services, you can do so using a compiler pass. More info on how to write custom compiler passes can be found at: http://symfony.com/doc/master/cookbook/service_container/compiler_passes.html

于 2013-03-12T15:42:51.223 回答
-2

您可以设置 $_ENV 变量并在之后获取

putenv("VAR=1");

并得到

getenv("VAR");
于 2019-01-29T09:29:10.293 回答