Chris J Allen
Chris J Allen

Reputation: 19207

Constructor dependency injection laravel 5

I've upgraded my laravel app from 4.2 to 5, and I'm getting the following error when trying to inject my dependency:

<?php namespace App\Classes\Api\Zurmo;

  use App\Classes\Api\Rest\ApiRestHelper;

  class Connector implements ConnectorInterface {

  public function __construct(ApiRestHelper $rest)
  {
     ...

Argument 1 passed to App\Classes\Api\Zurmo\Connector::__construct() must be an Instance of App\Classes\Api\Rest\ApiRestHelper, none given

As far as I can see, its looking ok, what am I missing here?

Upvotes: 0

Views: 1269

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 153150

If you want Laravel to resolve your dependencies automatically you have to instanciate the class through the Service Container:

$zurmo = App::make('App\Classes\Api\Zurmo\Connector');

Or with the app() function:

$zurmo = app('App\Classes\Api\Zurmo\Connector');

Note that you have to write out the full path of the class


Alternatively you could let Laravel inject the connector itself in the controller. For example:

use App\Classes\Api\Zurmo\Connector as Zurmo;

// ...

public function __construct(Zurmo $zurmo){
    $this->zurmo = $zurmo;
}

public function someAction(){
    $this->zurmo->doMagic();
}

Upvotes: 2

Related Questions