【发布时间】:2015-09-16 08:49:58
【问题描述】:
有人问了我的问题before。我还想访问位于我的个人库(在供应商目录中)中的全局配置(config/{,*.}{global,local}.php)。我想我找到的最接近的答案是here。我在课堂上创建了函数
public function getServiceConfig()
{
return array(
'factories' => array(
'funcservice' => function(\Zend\ServiceManager\ServiceLocatorInterface $sm) {
$config = $sm->get('config');
}
)
);
}
它可以工作,但是我不知道如何从结果中得到任何东西。
$config = $this->getServiceConfig();
print_r($config);
给我
Array
(
[factories] => Array
(
[funcservice] => Closure Object
(
[this] => Tools\Model\StandartFuncs Object
(
[eventIdentifier:protected] => Zend\Mvc\Controller\AbstractActionController
[plugins:protected] =>
[request:protected] =>
[response:protected] =>
[event:protected] =>
[events:protected] =>
[serviceLocator:protected] =>
)
[parameter] => Array
(
[$sm] => <required>
)
)
)
)
从$config = $this->getServiceConfig()->get('locales'); 我得到
致命错误:在非对象上调用成员函数 get()
【问题讨论】:
-
但是您需要的解释在错误描述中... $this->getServiceConfig() 返回一个数组,所以稍后您尝试调用 Array->get();当然 Array 不是对象,你不能调用 ->get() 方法......或者我不明白一个问题?试试 $config = $this->getServiceConfig(); $config['locales'];
-
这就是问题所在。我得到数组,但里面没有配置变量。
标签: php zend-framework2 config service-locator