Where to put custom settings in Zend Framework 2?

前端 未结 4 1462
有刺的猬
有刺的猬 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:07

    You use your module.config.php

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

    Inside your *Controller.php you'd call your settings via

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

    It's as simple as that :)

    0 讨论(0)
  • 2021-02-04 11:07

    If you look in config/application.config.php it says:

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

    So ZF2 by default will autoload configuration files from config/autoload/ - so for example you could have myapplication.global.php it would get picked up and added into the configuration.

    Evan.pro wrote a blog post that touches on this: https://web.archive.org/web/20140531023328/http://blog.evan.pro/environment-specific-configuration-in-zend-framework-2

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-04 11:23

    You can use any option from the following.

    Option 1

    Create one file called config/autoload/custom.global.php. In custom.global.php

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

    And in controller,

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

    Option 2

    In config\autoload\global.php or config\autoload\local.php

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

    And in controller,

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

    Option 3

    In module.config.php

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

    And in controller,

    $config = $this->getServiceLocator()->get('Config');
    echo $config['settings']['settingA'];
    
    0 讨论(0)
提交回复
热议问题