在Zend Framework 2中放置自定义设置的位置?

Rob*_*Rob 12 zend-framework2

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

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

return array(
    'settings' => array(
        'settingA' => 'foo',
        'settingB' => 'bar',
    ),
);
Run Code Online (Sandbox Code Playgroud)

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

提示非常感谢.

Mak*_*lin 16

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

module.config.php
module.customconfig.php
Run Code Online (Sandbox Code Playgroud)

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

return array(
    'settings' => array(
        'settingA' => 'foo',
        'settingB' => 'bar',
    ),
);
Run Code Online (Sandbox Code Playgroud)

然后你需要在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;
}
Run Code Online (Sandbox Code Playgroud)

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

 $config = $this->getServiceLocator()->get('config');
 $settings = $config["settings"];
Run Code Online (Sandbox Code Playgroud)

这对我有用,希望对你有所帮助.


Sam*_*Sam 12

你用你的 module.config.php

return array(
    'foo' => array(
        'bar' => 'baz'
    )

  //all default ZF Stuff
);
Run Code Online (Sandbox Code Playgroud)

你的内部*Controller.php通过你的方式调用你的设置

$config = $this->getServiceLocator()->get('config');
$config['foo'];
Run Code Online (Sandbox Code Playgroud)

就这么简单 :)


pra*_*ava 8

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

选项1

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

return array(
    'settings' => array(
        'settingA' => 'foo',
        'settingB' => 'bar'
    )
)
Run Code Online (Sandbox Code Playgroud)

在控制器中,

$config = $this->getServiceLocator()->get('Config');
echo $config['settings']['settingA'];
Run Code Online (Sandbox Code Playgroud)

选项2

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

return array(
    // Predefined settings if any
    'customsetting' => array(
        'settings' => array(
            'settingA' => 'foo',
            'settingB' => 'bar'
         )
    )
)
Run Code Online (Sandbox Code Playgroud)

在控制器中,

$config = $this->getServiceLocator()->get('Config');
echo $config['customsetting']['settings']['settingA'];
Run Code Online (Sandbox Code Playgroud)

选项3

在module.config.php中

return array(
    'settings' => array(
        'settingA' => 'foo',
        'settingB' => 'bar'
    )
)
Run Code Online (Sandbox Code Playgroud)

在控制器中,

$config = $this->getServiceLocator()->get('Config');
echo $config['settings']['settingA'];
Run Code Online (Sandbox Code Playgroud)