Zend framework2 如何在模型ZF2中调用getServiceLocator
我试图在模型方法中调用getServiceLocator,但它抛出以下错误 致命错误:对非对象调用成员函数get()Zend framework2 如何在模型ZF2中调用getServiceLocator,zend-framework2,Zend Framework2,我试图在模型方法中调用getServiceLocator,但它抛出以下错误 致命错误:对非对象调用成员函数get() 实际上,我正在使用$this->getServiceLocator()->get('Config');在导致上述致命错误的模型中,不应在模型类中使用ServiceLocator。请参阅以下答案:,或者在google中搜索“service locator antipattern”,以获取大量反对此做法的论据。您不能在模型类中直接使用ServiceLocator,但如果您想使用$th
实际上,我正在使用$this->getServiceLocator()->get('Config');在导致上述致命错误的模型中,不应在模型类中使用ServiceLocator。请参阅以下答案:,或者在google中搜索“service locator antipattern”,以获取大量反对此做法的论据。您不能在模型类中直接使用ServiceLocator,但如果您想使用
$this->getServiceLocator()->get('Config')代码>在模型上,然后
$config=$this->getServiceLocator()->get('config')代码>在控制器中分配并在madel中传递$config
$ModelTable->searchList($data, $config);
现在您可以调用/使用config的任何方法/函数。该错误表示您试图在非对象上执行函数。您还必须显示代码,以便我们能够说更多的话。我知道我可以这样做,但我不想遵循这种方法。查看内置的内容,以便直接在模型中使用getServiceLocator。