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