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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证