在Zend Framework 2应用程序中应该在哪里存储环境相关配置? [英] Where should be stored the environment dependent configs in a Zend Framework 2 application?

查看:174
本文介绍了在Zend Framework 2应用程序中应该在哪里存储环境相关配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ZF2应用程式包含/ nneds许多不同的设定档: /config/application.config.php / config / autoload / global .php /config/autoload/local.php / module / *** / config / module.config .php



现在我写了一个模块,覆盖了应用程序的缓存功能,需要不同的livetime值它的项目在我的本地/ dev和现场环境。我也希望能够根据环境切换缓存类型。



这样的东西应该放在哪里?在 /config/autoload/global.php /config/autoload/local.php 中?如果是的话:应该首先从模块类中的这些文件中检索(例如在 onBootstrap()方法)



(如果有人可以显示一个原始示例来保存和获取这样的配置数据,这也是很好的)。

$



对于缓存配置的东西,保持你的基本方法是正确的。生产值在全局文件中。这应该在你的VCS。 (编辑:但是,你应该可以忽略安全敏感的配置,如数据库密码。通过local.php将这些东西添加到生产中,以避免版本控制)。



在您的本地环境中,使用本地文件覆盖任何需要覆盖的内容。 IIRC ZendSkeletonApplication有一个.gitignore文件,它将忽略内置的任何本地配置 - 所以你的本地配置从不使它成为git。



但是,你不需要乱乱加载配置在引导像你一样。你可以从你的工厂方法中的serviceManager获取配置:

  public function getServiceConfig(){
try {
return array(
'factories'=> array(
...
'Zend\Cache\Adapter\MemcachedOptions'=> function($ serviceManager) {
return new MemcachedOptions(array(
//你可以从服务管理器获取你的配置
'ttl'=> $ serviceManager-> get('Config')[ 'cache_ttl'],
...
));
},
...

);
}
...
}

't stick'cache_ttl'作为顶级配置密钥。请尝试:



global.php

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

);

这样可以简化您的工厂,例如:

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

,你可以在local.php配置中覆盖任何你想要的。如果你想要做的是更改ttl(保留所有其他全局配置):



local.php

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


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.)

解决方案

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'=>...)
);

这篇关于在Zend Framework 2应用程序中应该在哪里存储环境相关配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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