【问题标题】:Impossible to call set() on a frozen ParameterBag无法在冻结的 ParameterBag 上调用 set()
【发布时间】:2013-03-12 14:59:16
【问题描述】:

在我的 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. 错误。 有什么帮助或线索吗?

【问题讨论】:

  • 你不能使用会话吗??

标签: symfony


【解决方案1】:

Container 编译完成后就不能修改了,这是在调用控制器之前完成的。

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

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

【讨论】:

  • 是的,我需要保存数据。我将为此使用一个新模型。它将是一个应用程序,安装在不同的用户位置,并且每个应用程序都需要不同的配置。谢谢!
  • 在这种情况下,我会考虑为每次安装使用不同的app/config/parameters.yml 文件。
【解决方案2】:

你可以设置 $_ENV 变量并在之后得到它

putenv("VAR=1");

得到

getenv("VAR");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-11
    相关资源
    最近更新 更多