Zend Framework 2 中的自定义设置放在哪里? [英] Where to put custom settings in Zend Framework 2?

查看:28
本文介绍了Zend Framework 2 中的自定义设置放在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some custom application specific settings, I want to put in a configuration file. Where would I put these? I considered /config/autoload/global.php and/or local.php. But I'm not sure which key(s) I should use in the config array to be sure not to override any system settings.

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

I was thinking of something like this (e.g. in global.php):

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

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

Is that an agreeable way? If so, how can I access the settings e.g. from within a controller?

非常感谢提示.

推荐答案

如果您需要为特定模块创建自定义配置文件,您可以在 module/CustomModule/config 文件夹中创建其他配置文件,像这样:

In case you need to create custom config file for specific module, you can create additional config file in module/CustomModule/config folder, something like this:

module.config.php
module.customconfig.php

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

This is content of your module.customconfig.php file:

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

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

Then you need to change getConfig() method in CustomModule/module.php file:

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;
}

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

Then you can use custom settings in controller:

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

它对我有用,希望对您有所帮助.

it is work for me and hope it help you.

这篇关于Zend Framework 2 中的自定义设置放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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