简体   繁体   English

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

[英]Fatal Error when using Service from vendor module in Zend framework 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. 我有一个带有1个模块的ZF2应用程序,我正在尝试使用位于“vendor”目录中的另一个名为“CommonRestClient”的模块中的类。

The directory structure of this "vendor" directory is as under: 此“vendor”目录的目录结构如下:

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: 所以,在主应用程序模块中,我试图在Model文件中使用上面的类,如下所示:

 <?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. 我已将MyApplication的application.config.php配置为使用“CommonRestClient”模块。

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 可捕获的致命错误:传递给MyApplication \\ Model \\的参数1 :: Users :: __ construct()必须是CommonRestClient \\ Service \\ CommonRestClient的实例,没有给定,在C:\\ Users \\ Public \\ myapp \\ myapplication \\ module \\ MyApplication \\ src中调用第27行的MyApplication \\ Controller \\ UsersController.php,第23行的C:\\ Users \\ Public \\ myapp \\ myapplication \\ module \\ MyApplication \\ src \\ MyApplication \\ Model \\ Users.php中定义

Can any one help me with what I could be missing here? 任何人都可以帮助我解决这里可能缺少的问题吗?

Thanks 谢谢

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. 在为Users类定义的构造函数中,您指定了从控制器创建实例时未提供的CommonRestClient的必需参数,这就是您收到错误的原因。

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: 或告诉ZF如何使用工厂做到这一点:

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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 zend Framework 2添加模块后出现致命错误 - zend framework 2 fatal error after add module 如何将Zend Framework 2中的模块目录从/ vendor更改为/ module? - How to change a module's directory in Zend Framework 2 from /vendor to /module? Zend Framework 2:致命错误-不在对象上下文中时使用$ this - Zend Framework 2: Fatal error - using $this when not in object context 在Zend Framework中使用Doctrine ORM时发生致命错误 - Fatal Error when using Doctrine ORM with Zend Framework Zend Framework 2 zftool致命错误 - Zend Framework 2 zftool fatal error 致命错误:在使用 zend 框架 2 创建表单时调用成员函数 setAttribute() 为 null - Fatal error: Call to a member function setAttribute() on null when creating form using zend framework 2 致命错误:在服务器中部署但不在本地部署时,在Zend Framework 2中找不到类 - Fatal error: Class not found in Zend Framework 2 when deployed in server but not locally 将Apigility v1.4注入Zend Framework 3时出现致命错误 - Fatal error when injecting Apigility v1.4 into Zend Framework 3 Zend Framework:尝试使用Zend Mail Transport发送带附件的多封电子邮件时出现致命错误 - Zend Framework: Fatal error when trying to use Zend Mail Transport to send multiple emails with attachments php - “致命错误:未找到类”zend框架1 - php - “Fatal error: Class not found” zend framework 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM