繁体   English   中英

zend framework 2 getServiceLocator空

[英]zend framework 2 getServiceLocator Empty

我一直在试图自己解决这个问题,我自己在网上阅读并在此处应用了大量的答案,但不幸的是没有结果。

我的zf2应用程序上有两个模块,一个称为服务,一个称为代理。 现在,在我的服务模块中,一切似乎都可以正常工作,我可以获取我的serviceLocator,从而获得我的配置,然后使用它。 但是,在代理模块的控制器中,我似乎无法执行相同操作。

这是我的AgentController的一部分:

use Zend\Mvc\Controller\AbstractActionController;

class AgentController extends AbstractActionController 
{

    protected $serviceLocator;

    public function ValidateAction()
    {

        $this->serviceLocator = $this->getServiceLocator()->get('config');
        //... Using the config
    }
}

在我的module.cofig.php中,我具有以下内容:

'controllers' => array(
    'invokables' => array(
        'Agent\Controller\Agent' => 'Agent\Controller\AgentController',
    ),
),

我已经尝试了许多解决方案:更改和向Module.php添加方法,更改module.config等。我在哪里错了?

谢谢,安德里亚

控制器类使用类变量$this->serviceLocator来保存服务定位器实例。 在您的示例中,您将config数组分配给该变量(因此将服务定位器实例替换为数组)。 随后对$this->getServiceLocator()调用将返回config数组,而不是服务定位器对象,这可能是导致您收到错误的原因。

我建议要么使用局部变量,要么:

public function ValidateAction()
{
    $config = $this->getServiceLocator()->get('config');
    //... Using the config
}

或分配给具有不同名称的类变量:

public function ValidateAction()
{
    $this->config = $this->getServiceLocator()->get('config');
    //... Using the config
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM