Laravel 5.0 - Where to use bindings from service providers?

In my App\Providers\RouteServiceProviderI created a method register:

public function register()
{
    $this->app->bindShared('JustTesting', function($app)
    {
        die('got here!');
        // return new MyClass;
    });
}

Where should I use this? I created a method in App\Http\Controllers\HomeController:

/**
 * ReflectionException in RouteDependencyResolverTrait.php line 53:
 * Class JustTesting does not exist
 *
 * @Get("/test")
 */
public function test(\JustTesting $test) {
    echo 'Hello';
}

But it does not work, I also can not use $ this-> app-> make ('JustTesting');

It works if I do the code below, but I would like to inject Inject into the controller.

/**
 * "got here!"
 *
 * @Get("/test")
 */
public function test() {
    \App::make('JustTesting');
}

How do I snap, how do I want? And if this is not allowed, why should I use the method bindShared?

+4
source share
1 answer

, ReflectionException, JustTesting , IoC Container .

, . JustTestingInterace MyClass , Laravel ", JustTestingInterface, MyClass."

RouteServiceProvider.php:

public function register()
{
    $this->app->bindShared('App\Namespace\JustTestingInterface', 'App\Namespace\MyClass');
}

:

use Illuminate\Routing\Controller;
use App\Namespace\JustTestingInterface;

class TestController extends Controller {

    public function test(JustTestingInterface $test)
    {
        // This should work
        dd($test);
    }
}
+1

All Articles