Laravel Interface and Repositories
What is a Laravel interface ?
Laravel interface is a contract for what methods will be used in a specific class. Actually, interfaces are not specific to Laravel, or even native PHP for that matter. Since we’ve been on a roll with Laravel, we’ll talk about interfaces in Laravel.
If you are developing small application, there is no need to use repositories. However, in case you want to build scalable applications and organize your code, repositories will help you a lot.
What is the Repository Pattern?
A repository represents an architectural layer that handles communication between the application and data source. It is a widely used pattern whose main point is that the application does not have to know which data source is implemented and how it is implemented. This makes it easier to switch to another data source or implement structural changes to the existing data source.
The repository pattern introduces a repository interface, which defines how the application and the data sources should communicate. Each data source has its own class which implements the repository interface. The controller class will call the methods defined in the repository interface and will not know how and from where the data is being fetched from.
Now, let’s talk about interfaces.
First, what are interfaces?
- Interfaces are 100% abstract classes — they have methods but the methods have no ‘guts’.
- Interfaces cannot be instantiated — they are a construct in OOP that allows you to inject ‘qualities’ into classes .. like abstract classes.
- Where an abstract class can have both empty and working/concrete methods, interface methods must all be shells — that is to say, it must be left to the class (using the interface) to flesh out the methods.
Remember: when a class uses/implements an interface, the class MUST define all the methods/functions of the interface otherwise the php engine will output an error.
PRIMARY PURPOSES OF AN INTERFACE:
- Interfaces allow you to define/create a common structure for your classes — to set a standard for objects.
- Interfaces solves the problem of single inheritance — they allow you to inject ‘qualities’ from multiple sources.
- Interfaces provide a flexible base/root structure that you don’t get with classes.
- Interfaces are great when you have multiple coders working on a project — you can set up a loose structure for programmers to follow and let them worry about the details.
Now, you may be wondering when I should use repository instead of interface or vice-versa.
WHEN SHOULD YOU MAKE AN INTEFACE?
- If you have a class that is never directly instantiated in your program, this is a good candidate for an interface. In other words, if you are creating a class to only serve as the parent to other classes, it should probably be made into an interface.
- When you know what methods a class should have but you are not sure what the details will be.
- When you want to quickly map out the basic structures of your classes to serve as a template for others to follow — keeps the code-base predictable and consistent.
Application — Custom Interface
App\Repositories\User
UserInterface.php :
<?php
namespace App\Repositories\User;
interface UserInterface
{
public function getAll();
public function find($id);
public function delete($id);
}
UserRepoServiceProvide.php :
<?php
namespace App\Repositories\User;
use Illuminate\Support\ServiceProvider;
class UserRepoServiceProvide extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->bind('App\Repositories\User\UserInterface', 'App\Repositories\User\UserInterface');
}
}
UserRepository.php
<?php
namespace App\Repositories\User;
use App\Models\User;
use App\Repositories\User\UserInterface as UserInterface;
class UserRepository implements UserInterface
{
public $user;
function __construct(User $user)
{
$this->user = $user;
}
public function getAll()
{
return $this->user->get();
}
public function find($id)
{
return $this->user->find($id);
}
public function delete($id)
{
return $this->user->delete($id);
}
}
App\Http\Controllers;
UserController.php
public function __construct(UserRepository $user)
{
$this->user = $user;
}public function index()
{
return $dene = $this->user->getAll();}