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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化