Where should be stored the environment dependent configs in a Zend Framework 2 application?

耗尽温柔 提交于 2019-12-11 11:09:15

问题


A ZF2 application contains/nneds a lot of different config files: /config/application.config.php, /config/autoload/global.php, /config/autoload/local.php, /module/***/config/module.config.php.

Now I've written a module, that covers the caching functionality for the application, and need different values for the livetime of its items in my local/dev and the live environment. I also would like to be able to switch the cache type dependent on the environment.

Where should such stuff be sored? In /config/autoload/global.php and /config/autoload/local.php? If yes: should it first retrieved from these files in the Module class (e.g. in the onBootstrap() method) or used directly, where it's needed?

(It also would be great, if someone could show a primitive example for saving and getting such config data.)


回答1:


The solution, I'm currently using is:

/config/autoload/global.php and/or /config/autoload/local.php

return array(
    // DB credentials
    'db' => array(
        'username'  => ...,
        'password'  => ...,
        'dbname'    => ...,
        'host'      => ...,
    ),
    'cache_ttl' => 'global/local value for cache live time',
);

Cache Module class

class Module {

    private $moduleConfig;

    public function onBootstrap(MvcEvent $mvcEvent) {
            $application = $mvcEvent->getParam('application');
            $this->moduleConfig = $application->getConfig();
    }

    ...

    public function getServiceConfig() {
        try {
            return array (
                'factories' => array(
                    ...
                    'Zend\Cache\Adapter\MemcachedOptions' => function ($serviceManager) {
                        return new MemcachedOptions(array(
                            'ttl'           => $this->moduleConfig['cache_ttl'],
                            ...
                        ));
                    },
                    ...
                )
            );
        }
        ...
    }
}

It works fine, but I'm pretty sure, that it's not the best practice / recomended way.




回答2:


Your basic approach is the correct one.

For cache configuration stuff, keep your production values in the global file. That should live in your VCS. (EDIT: however, you should probably omit security sensitive configuration such as database passwords. Add that stuff to production via a local.php to keep it out of version control).

In your local environment, use the local file to override anything that needs to be overridden. IIRC the ZendSkeletonApplication has a .gitignore file that will ignore any local configs built in -- so your local configuration never makes it into git.

However, you don't need to mess around with loading the config on bootstrap like you are. You can just grab the config from the serviceManager inside your factory method:

public function getServiceConfig() {
    try {
        return array (
            'factories' => array(
                ...
                'Zend\Cache\Adapter\MemcachedOptions' => function ($serviceManager) {
                    return new MemcachedOptions(array(
                        // you can just grab your config from the service-manager
                        'ttl'           => $serviceManager->get('Config')['cache_ttl'],
                        ...
                    ));
                },
                ...
            )
        );
    }
    ...
}

Also - I wouldn't stick 'cache_ttl' as a top-level config key. Instead, try:

global.php

return array(
    'cache' => array(
        'ttl' => ...,
        'servers' => ...,
        ...
    )
);

That simplifies your factory to just something like:

'Zend\Cache\Adapter\MemcachedOptions' => function ($serviceManager) {
    return new MemcachedOptions( $serviceManager->get('cache') );
},

and you can override whatever you want in your local.php config. If all you want to do is change the ttl (leaving all the other global configs):

local.php

return array(
    'cache' => array('ttl'=>...)
);


来源:https://stackoverflow.com/questions/15823473/where-should-be-stored-the-environment-dependent-configs-in-a-zend-framework-2-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!