duandongji2231 2014-02-26 19:55
浏览 42
已采纳

在Zend框架2中使用来自供应商模块的服务时出现致命错误

I have a ZF2 application with 1 module and I am trying to use a class from another module called "CommonRestClient" located in the "vendor" directory.

The directory structure of this "vendor" directory is as under:

vendor
-->CommonRestClient
---->config
------>module.config.php
-->src
---->CommonRestClient
------->Service
--------->CommonRestClient.php
-->Module.php
vendor\CommonRestClient\config\module.config.php
================================================
<?php
return array();
?>

vendor\src\Module.php
======================
<?php

namespace CommonRestClient;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;

use Zend\Http\Client as HttpClient;
use CommonRestClient\Service\CommonRestClient as CommonRestClient;

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }

    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'CommonRestClient\Service\CommonRestClient' => function($sm) {
                    $httpClient = $sm->get('HttpClient');
                    $httpRestJsonClient = new CommonRestClient($httpClient);
                    return $httpRestJsonClient;
                },
                'HttpClient' => function($sm) {
                    $httpClient = new HttpClient();
                    $httpClient->setAdapter('Zend\Http\Client\Adapter\Curl');
                    return $httpClient;
                },
            ),
        );
    }
}

vendor\src\CommonRestClient\Service\CommonRestClient.php
=========================================================
<?php

namespace CommonRestClient\Service;

use Zend\Http\Client as HttpClient;
use Zend\Http\Request;
use Zend\Stdlib\Parameters;

class CommonRestClient
{
    protected $httpClient;

    public function __construct(HttpClient $httpClient)
    {
        $this->httpClient = $httpClient;
    }

    public function get($url)
    {
        return $this->dispatchRequestAndDecodeResponse($url, "GET");
    }

    public function post($url, $data)
    {
        return $this->dispatchRequestAndDecodeResponse($url, "POST", $data);
    }

    public function put($url, $data)
    {
        return $this->dispatchRequestAndDecodeResponse($url, "PUT", $data);
    }

    public function delete($url)
    {
        return $this->dispatchRequestAndDecodeResponse($url, "DELETE");
    }

    protected function dispatchRequestAndDecodeResponse($url, $method, $data = null)
    {
        $request = new Request();
        $request->getHeaders()->addHeaders(array(
            'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8'
        ));
        $request->setUri($url);
        $request->setMethod($method);

        if ($data){
            $request->setPost(new Parameters($data));
        }

        $response = $this->httpClient->dispatch($request);
        # should interogate response status, throwing appropiate exceptions for error codes
        return json_decode($response->getBody(), true);
    }
}

So, in the main application module, I am trying to use the above class in a Model file, which looks like this:

<?php

namespace MyApplication\Model;

use CommonRestClient\Service\CommonRestClient as CommonRestClient;

class Users
{    
    protected $commonRestClient;

    public function __construct(CommonRestClient $commonRestClient)
    {
        $this->commonRestClient = $commonRestClient;
    }

    public function getListOfUsers()
    {
        $url = 'http://xyz.google.com/getusers';
        $jsonResponse = $this->commonRestClient->get($url);
        return $jsonResponse;
    }
}

I have configured MyApplication's application.config.php to use the "CommonRestClient" module.

The error I am recieving is:

Catchable fatal error: Argument 1 passed to MyApplication\Model\Users::__construct() must be an instance of CommonRestClient\Service\CommonRestClient, none given, called in C:\Users\Public\myapp\myapplication\module\MyApplication\src\MyApplication\Controller\UsersController.php on line 27 and defined in C:\Users\Public\myapp\myapplication\module\MyApplication\src\MyApplication\Model\Users.php on line 23

Can any one help me with what I could be missing here?

Thanks

  • 写回答

1条回答 默认 最新

  • dongpan5289 2014-02-26 21:07
    关注

    In the constructor you defined for your Users class you specified a required argument of CommonRestClient which you are not supplying when creating the instance from your controller, so that's why you are getting the error.

    You can either supply the argument yourself:

    $users = new Users($this->getServiceLocator()->get('CommonRestClient\Service\CommonRestClient'));
    

    or tell ZF how to do that using a factory:

    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'Users' => function($sm) {
                    $commonRestClient = $sm->get('CommonRestClient\Service\CommonRestClient');
                    $users = new Users($commonRestClient);
                    return $users;
                },
            ),
        );
    }
    

    and then use the service locator to create the instance for you:

    $users = $this->getServiceLocator()->get('Users');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算