2

I'm kind of confused of how to use contracts. I think that's because I haven't used unit-testing so that it's not obvious for me how contracts work.

Let's have look at this code:

use Illuminate\Contracts\Auth\Guard;

...

public function __construct(Guard $auth)
{
    $this->auth = $auth;

    $this->middleware('guest', ['except' => 'getLogout']);
}

public function postRegister(RegisterRequest $request)
{
    // Registration form is valid, create user...

    $this->auth->login($user);

    return redirect('/');
}
  1. So how do I know which class implements login method of contract in this line: $this->auth->login($user) ? And how can I change the class if I want to use my own?

  2. In laravel 4 I wrote Auth::user() as an example and I used it everywhere in any controller and it worked. Now I should inject a contract inside a controller method and use it like $auth->user?

  3. Also, If I get it right, contracts are used for making an abstraction. Okay, so, if I want to build a new interface for my own class and then have multiple classes that implement my interface, where should I write the code? I can't think of an example but lets imagine I need to implement an interface for enabling/disabling a lamp, and I have two methods like on() and off() and I have multiple ways to do that. Do I need to create new contract for that?

Victor
  • 5,073
  • 15
  • 68
  • 120

1 Answers1

7

I hope I can make this a bit clearer for you...

Ad.1. You can check default binding at /vendor/laravel/framework/src/Illuminate/Foundation/Application.php (method registerCoreContainerAliases around line 792). If you want to create your own class or extend existing I recommend looking at How to extend Laravel's Auth Guard class? or http://laravel.com/docs/master/extending (this one is more about Laravel 4.x but might give you an idea).

Ad.2. Actually you can still use Auth::user() but I inject a contract in constructor or a method and call it like $this->auth->user or $auth->user.

Ad.3. I have a /app/Repositories folder where I put my interfaces and implementations, so to follow your example I would create subfolder Lamp and I would create LampInterface with on() and off() methods, then I would create something like Lamp.php that implements LampInterface. Next I would create a service provider in /app/Providers, like LampServiceProvider.php with binding:

namespace Apps\Providers;

use Illuminate\Support\ServiceProvider;

class LampServiceProvider extends ServiceProvider {

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton(
            'App\Repositories\Lamp\LampInterface',
            'App\Repositories\Lamp\Lamp'
        );
    }
} 

After that I would register new service provider in /app/config/app.php and finally I can inject my interface like:

public function switchLampOn(App\Repository\Lamp\LampInterface $lamp)
{
    $lamp->on();
}
Community
  • 1
  • 1
Raff W
  • 188
  • 2
  • 9