开发者

How do you work with "global" configuration within ZF?

I'm not really sure what the best way to use global configuration options. For example, if when a file is uploaded I want to move it in the correct folder.

  • I can hardcode the path, but 开发者_StackOverflowit's not really the best thing.
  • I can use a CONSTANT
  • I can use config.ini and sets some common config options. Maybe then register a config object in Registry

How do you do? Any advice?


A Zend_Config object in the Registry is the usual method that ZF follows here. Many of the ZF classes can accept Configs, and there's pretty much no better way to deal with it within ZF.

(Just remember, the Registry pattern is little more than a glorified global anyway.)


Create a config.ini, and within it separate your configurations like so:

[development]
;File Upload settings
FileUpload.path = /some/path

[production]
;File Upload settings
FileUpload.path = /production/path

Now somewhere in your Bootstrap.php, you can do this:

$config = new Zend_Config_Ini(
    self::$root . '/config/config.ini',
    'development'
);
self::$registry->configuration = $config;

And in any controller:

$config = Zend_Registry::get('configuration');
echo $config->FileUpload->path;


I use Zend_Registry::set('foo', '/path/to/correct/folder') once, and then call it whenever I need it with Zend_Registry::get('foo') anytime I need it. Works great!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜