In my Laravel 5 application, I registered a context binding for an interface in a service provider, for example:
$this->app->when('App\Http\Controllers\MyController')
->needs('App\Contracts\MyRepositoryInterface')
->give('App\Repositories\MyRepostory');
Inside the controller MyController, I have a method index()in which I am trying to enter MyRepositoryInterfaceas follows:
public function index(App\Contracts\MyRepositoryInterface $repo)
{
}
The problem is that the above does not work and gives this error:
BindingResolutionException in line Container.php 754:
Target [App \ Contracts \ MyRepositoryInterface] does not work.
However, if I change the context binding to a regular binding , as shown below, it works:
$this->app->bind(
'App\Contracts\MyRepositoryInterface',
'App\Repositories\MyRepository'
);
, , , :
public function __constructor(App\Contracts\MyRepositoryInterface $repo)
{
}
: ( )? , Laravel 5?
- ?
, !