Jake
Jake

Reputation: 26117

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.

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

Upvotes: 0

Views: 983

Answers (1)

Tim Fountain
Tim Fountain

Reputation: 33148

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

Upvotes: 2

Related Questions