dongshiru5913 2014-04-03 10:33
浏览 36
已采纳

在构造ZF2中使用Service

I must have ServiceLocator in my controller's construct, but I can't.

In my method I can use to get DB params :

$this->getServiceLocator()

But, this method doesn't work in my __construct.

So, I tried to add that in my Module.php but it doesn't look working :

public function getControllerConfig() {
    return array(
        'factories' => array(
            'validechoixoptions'    => function(ControllerManager $cm) {
                $sm   = $cm->getServiceLocator();
                $controller = new \Validechoixoptions\Controller\Index($sm);
                return $controller;
            },
        ),
    );
}

What's wrong ? I don't understand how use this factories.

EDIT

Thanks for this fast answer

I tried it :

'Validechoixoptions'=> function(ControllerManager $cm) {
    $sm = $cm->getServiceLocator();

    $sm = $sm->get('webapp');

    $controller = new \Validechoixoptions\Controller\IndexController($sm);
    return $controller;
}

And I have webapp in this method of Module.php :

public function getServiceConfig()
{
    return array(
        'factories' => array(
            'webapp' => new AdapterServiceFactory('webapp'),

But, I have an error :

: Missing argument 1 for Validechoixoptions\Controller\IndexController::__construct(), called in /usr/local/zend/share/ZendFramework2/library/Zend/ServiceManager/AbstractPluginManager.php on line 170 and defined inValidechoixoptions/Controller/IndexController.php on line 32

I need serviceLocator because I must create an instance of my model in my __construct of my controller and I use serviceLocator like a param of __construct model

Thanks

EDIT 2

Big thanks, it works, I didn't comment

'Validechoixoptions\Controller\Index' => 'Validechoixoptions\Controller\IndexController'

in config/module.config.php

:)

  • 写回答

1条回答 默认 最新

  • duanbarong4321 2014-04-03 11:43
    关注

    You can't call $this->getServiceLocator() in the constructor as the service locator hasn't been populated at that point. Instead of trying to force the service locator in as in your example, use the factory to get the dependencies you need from the service locator and pass them into the controller via. the constructor:

    public function getControllerConfig() {
        return array(
            'factories' => array(
                'validechoixoptions'=> function(ControllerManager $cm) {
                    $sm = $cm->getServiceLocator();
    
                    $foo = $sm->get('Foo');
                    $bar = $sm->get('Bar');
    
                    $controller = new \Validechoixoptions\Controller\IndexController($foo, $bar);
                    return $controller;
                },
            ),
        );
    }
    

    and then your controller:

    class IndexController extends AbstractActionController
    {
        protected $foo;
    
        protected $bar;
    
        public function __construct($foo, $bar)
        {
            $this->foo = $foo;
            $this->bar = $bar;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?