如何在Model ZF2中调用getServiceLocator

时间:2018-08-22 10:59:40

标签: zend-framework2

我正在尝试在模型方法中调用getServiceLocator,但是它抛出以下错误 致命错误:在非对象上调用成员函数get()

实际上,我正在使用$ this-> getServiceLocator()-> get('Config');在模型中导致上述致命错误

2 个答案:

答案 0 :(得分:1)

您不应在模型类中使用ServiceLocator。请参考以下答案:https://stackoverflow.com/a/32858171/1644937,或者只是在Google中搜索:“服务定位器反模式”,以获取大量反对这种做法的论点。

答案 1 :(得分:0)

您可能不会在模型类中直接使用ServiceLocator,但是如果要在模型上使用$this->getServiceLocator()->get('Config');,则

$config = $this->getServiceLocator()->get('Config');在控制器中分配并在$ makel中传递$ config

$ModelTable->searchList($data, $config);

现在您调用/使用配置的任何方法/功能。

相关问题