访问Zend Framework 2中的模块配置 [英] Access to module config in Zend Framework 2

查看:138
本文介绍了访问Zend Framework 2中的模块配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何从控制器访问我的模块配置?

How I can get access to my module config from the controller?

推荐答案

我真的很惊讶这是多么模糊因为我有完全相同的问题,找不到确定的答案。人们会认为ZF2文档会对此有所了解。无论如何,使用试用错误,我发现这个非常简单的答案:

I am really surprised at how obscure this is, because I had exactly the same problem and could not find a definitive answer. One would think the ZF2 documentation would say something about this. Anyhow, using trial and error, I came across this extremely simple answer:

$config = $this->getServiceLocator()->get('Config');



内部模块类函数( Module.php 文件):



Inside Module class functions (the Module.php file):

$config = $e->getApplication()->getServiceManager()->get('Config');

$ e code> Zend\Mvc\MvcEvent

whereas $e is an instance of Zend\Mvc\MvcEvent

一般来说,配置自从您可以访问全局服务管理器的任何地方访问,因为配置阵列已注册为名为 Config 的服务。 (注意大写 C 。)

In general, the config is accessible from anywhere you have access to the global service manager since the config array is registered as a service named Config. (Note the uppercase C.)

这将返回一个数组的application.config.php(global and local)和你的module.config.php。然后,您可以根据需要访问数组元素。

This returns an array of the union of application.config.php (global and local) and your module.config.php. You can then access the array elements as you need to.

即使OP现在很旧,我希望这可以节省一小时或更多时间,让我得到这个答案。

Even though the OP is quite old now, I hope this saves someone the hour or more it took me to get to this answer.

这篇关于访问Zend Framework 2中的模块配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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