[英]Access config global in Zend Framework 2
我想访问位于我的个人库(在vendor
目录中)的全局配置( config/{,*.}{global,local}.php
)。 但是,尽管我在网络上进行搜索,但还是没有成功实现这一目标。
我知道如何访问模块的配置文件:
\MyModule\Module::getConfig()
我知道如何从控制器访问全局配置:
$this->getServiceLocator()->get('config');
但是如何从供应商目录中的文件访问这些文件? 我的库没有扩展任何内容,这可能是问题的根源吗? 我可以使用静态方法来访问这些吗?
谢谢。
您可以通过serviceManager获得它。 在控制器中:
$config = $this->getServiceLocator()->get('Config');
否则,只需使用Module.php中的eaxmple,就需要Service Manager:
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getServiceConfig()
{
return array(
'factories' => array(
'mail.transport' => function($sm) {
$config = $sm->get('Config');
switch($config['mail']['transport']['type']){
..................
简单的方法做到这一点。 不仅在模块中,而且在任何地方。
$config = new \Zend\Config\Config( include APPLICATION_PATH.'/config/autoload/global.php' );
echo $config->myconfig;
在public / index.php中定义APPLICATION_PATH
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.