Pregunta

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');
        });
    }
¿Fue útil?

Solución

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 {

    ...

}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top