Where to put custom settings in Zend Framework 2?

前端 未结 4 1611
有刺的猬
有刺的猬 2021-02-04 10:47

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

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-04 11:10

    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
    

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

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

    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.

提交回复
热议问题