如何在Symfony2中动态设置参数? [英] How can I dynamically set a parameter in Symfony2?

查看:74
本文介绍了如何在Symfony2中动态设置参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Symfony2中动态设置一个参数(不能在parameters.yml文件中静态设置).我的方法是使用EventListener:

I'm trying to dynamically set a parameter in Symfony2 (that I cannot statically set in my parameters.yml file). My approach is to use an EventListener:

namespace Acme\AcmeBundle\EventListener;

use Symfony\Component\DependencyInjection\Container;

class AcmeListener
{
    private $container;

    public function __construct(Container $container)
    {
        $this->container = $container;
    }

    public function onKernelRequest()
    {
        // Dynamically fetch $bar
        $bar = fetch('foobar');

        // Set parameter
        $this->container->setParameter('foo', $bar);
    }
}

config.yml中的服务定义如下:

service:
    kernel.listener.acme_listener:
        class: Acme\AcmeBundle\EventListener\AcmeListener
        tags:
            - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
        arguments: [ '@service_container' ]

问题是,我得到一个例外:

The problem is, I get an exception:

LogicException:无法在冻结的ParameterBag上调用set().

LogicException: Impossible to call set() on a frozen ParameterBag.

如何解决此异常,或者您看到动态设置参数的另一种方法?

How can I work around this exception or do you see another way to dynamically set a parameter?

推荐答案

容器参数规则是:

您只能在编译容器之前设置参数

You can only set a parameter before the container is compiled

在不认为容器具有动态参数的前提下,如何解决此问题取决于您的需要.

How to remedy the problem depends on your needs with the premise that the container is not thought to have dynamic parameters.

  1. 创建自定义动态选项"服务并将其注入其他服务,这样您还可以管理数据库中的参数(例如wordpress wp_options),但是我不知道这样做的捆绑软件.对于现有服务(例如邮递员),您可以使用配置器.

在参数更改时使缓存无效这里是一种简单的方法因此,当您重新加载页面时,将重新构建容器.如果参数频繁更改,则可能会导致频繁重新加载缓存的风险,如果负载很大,这将成为问题.

invalidate the cache when parameters changes here an easy method so when you reload the page the container is rebuilt. If the parameters change frequently risks to reload the cache frequently and this becomes a problem if you have large loads.

如果选择第二个选项,则需要先设置参数,然后再将其填充到容器中,这样您就可以:

if you choose the second option you need to set the parameters before it is filled in the container, so you can:

  • 在清除缓存之前参数发生更改时,导出加载到app/config/config.yml中的自定义yaml文件,这样您就可以从其他服务获取数据
  • 将参数加载到包扩展名此处为食谱,这样,您将无法访问其他服务来获取数据,扩展名只能访问containerbuilder
  • export in a custom yaml file loaded in app/config/config.yml when parameters changes before you clear the cache, in this way you can get the data from other services
  • load parameters in a bundle extension here the cookbook, in this way you can't access to other services to get the data, the extension can access only to the containerbuilder

但是,我建议使用选项1(选项服务和配置程序),因为(我重复一遍)该容器不具有动态参数,但是它具有使用来自任何来源的数据的自定义动态服务配置程序的能力.

I suggest, however, option 1 (options service and configurators) because (I repeat) the container is not thought to have dynamic parameters but it offers the ability to have custom dynamic service configurators that use data from any source.

这篇关于如何在Symfony2中动态设置参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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