无法在冻结的 ParameterBag 上调用 set() [英] Impossible to call set() on a frozen ParameterBag

查看:24
本文介绍了无法在冻结的 ParameterBag 上调用 set()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 config.yml 中有这个:

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, like this:

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

我遇到无法在冻结的 ParameterBag 上调用 set(). 一直错误.有什么帮助或线索吗?

I´m getting Impossible to call set() on a frozen ParameterBag. error all the time. Any help or clue?

推荐答案

Container 一旦被编译,就不能修改,这是在调用控制器之前完成的.

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

DIC 参数用于配置目的 - 不能替代全局变量.此外,您似乎想要坚持某种永久修改.在这种情况下,如果它是针对每个用户的修改,请考虑使用 session;如果它应该是应用程序范围的,请考虑使用它(例如,保存到数据库中).

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.

如果您需要修改 DIC 参数或服务,您可以使用编译器传递来完成.有关如何编写自定义编译器传递的更多信息,请访问:http://symfony.com/doc/master/cookbook/service_container/compiler_passes.html

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

这篇关于无法在冻结的 ParameterBag 上调用 set()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆