【问题标题】:Where to put custom settings in Zend Framework 2?在 Zend Framework 2 中的何处放置自定义设置?
【发布时间】:2012-10-20 22:08:19
【问题描述】:

我有一些自定义应用程序特定设置,我想放入一个配置文件。我会把这些放在哪里?我考虑过/config/autoload/global.php 和/或local.php。但我不确定我应该在配置数组中使用哪个键来确保不会覆盖任何系统设置。

我在想这样的事情(例如在 global.php 中):

return array(
    'settings' => array(
        'settingA' => 'foo',
        'settingB' => 'bar',
    ),
);

这是一种令人愉快的方式吗?如果是这样,我如何访问设置,例如从控制器内部?

非常感谢提示。

【问题讨论】:

    标签: zend-framework2


    【解决方案1】:

    如果您需要为特定模块创建自定义配置文件,您可以在 module/CustomModule/config 文件夹中创建额外的配置文件,如下所示:

    module.config.php
    module.customconfig.php
    

    这是您的 module.customconfig.php 文件的内容:

    return array(
        'settings' => array(
            'settingA' => 'foo',
            'settingB' => 'bar',
        ),
    );
    

    然后你需要在 CustomModule/module.php 文件中更改 getConfig() 方法:

    public function getConfig() {
        $config = array();
        $configFiles = array(
            include __DIR__ . '/config/module.config.php',
            include __DIR__ . '/config/module.customconfig.php',
        );
        foreach ($configFiles as $file) {
            $config = \Zend\Stdlib\ArrayUtils::merge($config, $file);
        }
        return $config;
    }
    

    然后你可以在控制器中使用自定义设置:

     $config = $this->getServiceLocator()->get('config');
     $settings = $config["settings"];
    

    这对我有用,希望对您有所帮助。

    【讨论】:

    • 我认为这是 Sam 的答案更好的答案。尽管这两个文件被合并并最终产生相同的结果,但这会将核心配置与用户配置分开。我认为这会导致更少的错误。
    • 只是补充一点:应该提供用户可以编辑的配置文件是正确的。然而,以这里概述的方式这样做是“错误的”(阅读:不是最好的方式)。社区默认设置是有一个“module.config.php”,所有设置都具有非常合理的默认设置,然后在 config 文件夹中提供一个“$modulename.config.php.dist”。然后,此 dist 文件包含所有重复的用户设置,并对它们的作用进行了大量解释。然后将该文件手动复制到应用程序的 /config/autoload 目录中。
    【解决方案2】:

    你使用你的module.config.php

    return array(
        'foo' => array(
            'bar' => 'baz'
        )
    
      //all default ZF Stuff
    );
    

    在您的 *Controller.php 中,您可以通过以下方式调用您的设置

    $config = $this->getServiceLocator()->get('config');
    $config['foo'];
    

    就这么简单:)

    【讨论】:

    • 嗨,山姆,感谢您的回答。我所说的设置可能会被多个模块使用。 module.config.php 不适合这些,不是吗?其次:我怎么知道数组键 'foo' 不是 ZF 的默认键?是否有 ZF2 使用的保留配置键的完整列表?
    • 目前还没有这样的列表:S 显然,为许多不同模块提供的配置可以存储在单独的配置中,就像 DrBeza 指出的那样。访问此类配置的方式相同。
    • 太好了,我就是这样做的。感谢您和 DrBeza!
    【解决方案3】:

    您可以使用以下任何选项。

    选项 1

    创建一个名为 config/autoload/custom.global.php 的文件。在 custom.global.php 中

    return array(
        'settings' => array(
            'settingA' => 'foo',
            'settingB' => 'bar'
        )
    )
    

    在控制器中,

    $config = $this->getServiceLocator()->get('Config');
    echo $config['settings']['settingA'];
    

    选项 2

    在 config\autoload\global.php 或 config\autoload\local.php 中

    return array(
        // Predefined settings if any
        'customsetting' => array(
            'settings' => array(
                'settingA' => 'foo',
                'settingB' => 'bar'
             )
        )
    )
    

    在控制器中,

    $config = $this->getServiceLocator()->get('Config');
    echo $config['customsetting']['settings']['settingA'];
    

    选项 3

    在module.config.php中

    return array(
        'settings' => array(
            'settingA' => 'foo',
            'settingB' => 'bar'
        )
    )
    

    在控制器中,

    $config = $this->getServiceLocator()->get('Config');
    echo $config['settings']['settingA'];
    

    【讨论】:

      【解决方案4】:

      如果你查看config/application.config.php,它会说:

      'config_glob_paths'    => array(
          'config/autoload/{,*.}{global,local}.php',
      ),
      

      所以默认情况下 ZF2 会从 config/autoload/ 自动加载配置文件 - 例如,您可以让 myapplication.global.php 被拾取并添加到配置中。

      Evan.pro 写了一篇博客文章,涉及到这一点:https://web.archive.org/web/20140531023328/http://blog.evan.pro/environment-specific-configuration-in-zend-framework-2

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-16
        • 2012-08-26
        • 1970-01-01
        • 2013-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多