php - Laravel 5.0 - Where to use bindings from Service Providers? -
in app\providers\routeserviceprovider
did create method register
:
public function register() { $this->app->bindshared('justtesting', function($app) { die('got here!'); // homecoming new myclass; }); }
where should utilize that? did create method in app\http\controllers\homecontroller
:
/** * reflectionexception in routedependencyresolvertrait.php line 53: * class justtesting not exist * * @get("/test") */ public function test(\justtesting $test) { echo 'hello'; }
but didn't works, can't utilize $this->app->make('justtesting');
it works if code below, inject controller.
/** * "got here!" * * @get("/test") */ public function test() { \app::make('justtesting'); }
how should bind want to? , if it's not allowed, why should utilize bindshared
method?
it appears though first controller route throwing reflectionexception because object justtesting
not exist @ time ioc container attempting resolve it.
also, should code interface. binding justtestinginterace
myclass
laravel knows "ok, when implementation of justtestinginterface
requested, should resolve myclass
."
routeserviceprovider.php:
public function register() { $this->app->bindshared('app\namespace\justtestinginterface', 'app\namespace\myclass'); }
inside of controller:
use illuminate\routing\controller; utilize app\namespace\justtestinginterface; class testcontroller extends controller { public function test(justtestinginterface $test) { // should work dd($test); } }
php laravel laravel-routing laravel-5
No comments:
Post a Comment