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('/');
}
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?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
?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()
andoff()
and I have multiple ways to do that. Do I need to create new contract for that?