alex29
6 months ago

How do I implement an interface onto a Laravel project properly?

Posted 6 months ago by alex29

Hi, so I've recently just started working on a new project and I figured I would try implementing the SOLID principle to make my codebase more extensible. I started out with something simple: showing a registration form depending on the account type specified in the route. Here is what I've come up with so far:

// routes/web.php
Route::get('{accountType}/register', '[email protected]');

// RegisterController
public $accountType;

public function __construct(AccountTypeInterface $accountType)
{
    $this->accountType = $accountType;
}

public function showRegistrationForm()
{
    $this->accountType->showRegistrationForm();
}

// EmployerAccountType.php
public function showRegistrationForm()
{
    return view('employers.auth.register');
}

// EmployeeAccountType.php
public function showRegistrationForm()
{
    return view('employees.auth.register');
}

The problem I am facing right now is I am not quite sure how the RegisterController is going to know which AccountType to instantiate (Employee or Employer), and I did some reading around in the docs and eventually arrived at the Service Container and Service Provider, although it was a little confusing to be honest but I did understand that I should bind the Interface to an instance of either EmployeeAccountType or EmployerAccountType, but I need both, not one or the other, how should I go about doing this? Thanks for the help.

Please sign in or create an account to participate in this conversation.