1

I have a Facade Player where i set up a function info, this function will give me the information of the players, the my issue is when i call that function by dependency injection it throw me the error:

Call to undefined method Team\Player\Facades\Player::info()

instead if i call that function doing Player::info(); it Work! Why with the depency throw me that error?

following my class Player

class Player {

use Team\Player\Models\User;
use Team\Player\Models\Team;
use Team\Player\Models\Fighter;

  public function info($user_id)
  {
    return Fighter::with('team','user')->where('player_id','=',$user_id)->first();
  }

}

My controller where i try to call the dependecy

class MatchController extends BaseController {

    protected $match;
    protected $player;

    function __construct(Match $match,Player $player) { // dependency injection
        $this->match = $match;
        $this->player = $player;
    }

    public function getInfoPlayer() 
    {
        $player_id = Input::get('user_id');
        $player = $this->player->info($player_id);
        return View::make('site/team/event/scripts/infoPlayer')->withPlayer($player);
    }


}

and my service provider where i bind my class

public function register()
    {

         $this->app['player'] = $this->app->share(function($app)
      {
        return new Player;
      });

         $this->app->booting(function()
        {
          $loader = \Illuminate\Foundation\AliasLoader::getInstance();
          $loader->alias('Player', 'Team\Player\Facades\Player');
        });
    }
Fabrizio Fenoglio
  • 5,767
  • 14
  • 38
  • 75

1 Answers1

3

You're not showing ALL namespaces and uses clauses from your files, where the conflict is.

But I can do a little guess from what you gave us:

If you receive

 Call to undefined method Team\Player\Facades\Player::info()

When you do

$player = $this->player->info($player_id);

Is because

function __construct(Match $match, Player $player) { // dependency injection
}

Is telling, somehow, Laravel to instantiate

Team\Player\Facades\Player

Instead of using the already instantiated:

Team\Player\Player

Which is

$this->app['player'];

So, one possibility is having a

uses Team\Player\Facades\Player;

In your MatchController class. Where you should have

use Player;

or

use Team\Player\Player;

But this will make Laravel to inject a new instance of Team\Player\Player and not $this->app['player'], to do that, you would have to

function __construct(Match $match, PlayerInterface $player) {}

And then

App::bind('PlayerInterface', function($app) {
    return $app['player'];
});

Well... something like that.

EDIT

You'll have to create that inteface, because PHP will complaint about not finding it.

The best place to put this binding is in its own ´app/bindings.php´ file, but you'll have to load it in your app/start/global.php:

require app_path().'/filters.php';
require app_path().'/bindings.php';

Your controller should look like this:

uses Team\Player\PlayerInterface;

class MatchController extends BaseController {

    ...

    function __construct(Match $match, PlayerInterface $player) 
    {
       ...
    }

    public function getInfoPlayer() 
    {
       ...
    }

}

You Player class must implement that interface:

class Player implements PlayerInterface {

    ...

}
Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
  • I can just say thank you very much!!!! This is a good explanation but i have to ask something about. You bind PlayerInterface, but i haven't that Inteface i should create one? wich is the best place for bind the player interface? it's fine on the serviceProvider file? – Fabrizio Fenoglio Jan 22 '14 at 16:21
  • 1
    Yes, you'll have to create an (empty) interface, because PHP will complaint about not finding that class or interface. Just edited to answer your questions properly. – Antonio Carlos Ribeiro Jan 22 '14 at 16:47
  • Ok, Thank you! But i created that interface and bind it how you did on the answer adding the right namespace but it come with an error, Class PlayerInterface does not exist, Illuminate\\Container\\Container.php","line":522. Sorry but i'm trying to understand better. Cheers! – Fabrizio Fenoglio Jan 22 '14 at 17:05
  • Looks like you're now just missing a uses, edited again. – Antonio Carlos Ribeiro Jan 22 '14 at 17:17
  • Thanks!! we are close thank you for the patience but now it come with this error: Argument 2 passed to MatchController::__construct() must implement interface Team\\Player\\PlayerInterface, instance of Team\\Player\\Player – Fabrizio Fenoglio Jan 22 '14 at 17:32
  • Yeah, sorry, I forgot to add that too. It's exactly what the error is saying, your Player class must implement that Interface. Edited. – Antonio Carlos Ribeiro Jan 22 '14 at 17:36
  • Hello antonio, i'm very sorry to disturb you again but i have another small issue with the IoC. I'll very glad if you can find 3 minutes for help me. http://stackoverflow.com/questions/21311716/interface-laravel-doesnt-bind-why – Fabrizio Fenoglio Jan 23 '14 at 15:17