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

报告相同问题?

悬赏问题

  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法