[英]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.