Home > Article > Backend Development > Laravel Repository Pattern
Repository Pattern
To keep the code clean and readable, it is very useful to use Repository Pattern
. In fact, we don’t have to use Laravel
just to use this special design pattern. However, in the following scenario, we will use the OOP
framework Laravel
to show how to use repositories
to make our Controller
Layers are less verbose, more decoupled and easier to read. Let’s take a closer look.
Do not use repositories
In fact, it is not necessary to use Repositories
. You can complete most things in your application without using this design pattern. However, as time goes by, you may Putting yourself into a blind spot, for example, not choosing to use Repositories
will make your application testing very difficult, (swapping out implementations) the specific implementation will become very complicated, let's look at an example below. HousesController.php
<?php class HousesController extends BaseController { public function index() { $houses = House::all(); return View::make('houses.index',compact('houses')); } public function create() { return View::make('houses.create'); } public function show($id) { $house = House::find($id); return View::make('houses.show',compact('house')); } }
This is a very typical piece of code that uses Eloquent
to interact with the database. This code works normally, but the controller
layer will be tightly coupled to Eloquent
of. Here we can inject a repository
to create a decoupled version of the code. This decoupled version of the code can make the specific implementation of subsequent programs simpler.
Using repositories
It actually requires quite a few steps to complete the entire repository
pattern, but once you complete it a few times it will naturally become a habit. Below we will introduce each step in detail.
Repository
folder First we need to create our own Repository
folderrepositories
in the app
folder, and then each file in the folder must set the corresponding namespace.
Interface
class The second step is to create the corresponding interface, which determines the related methods that our repository
class must implement, as shown in the example below, where the namespace is emphasized again Be sure to remember to add it. HouseRepositoryInterface.php
<?php namespace App\Repositories; interface HouseRepositoryInterface { public function selectAll(); public function find($id); }
Repository
classNow we can create our repository
class to do the work for us. In this class file we can put most of our Most database queries are put in there, no matter how complex. As in the example belowDbHouseRepository.php
<?php namespace App\Repositories; use House; class DbHouseRepository implements HouseRepositoryInterface { public function selectAll() { return House::all(); } public function find($id) { return House::find($id); } }
<code><span><span> </span></span></code>
First you need to understand the so-called service provider, please refer to the manual service providerBackendServiceProvider.php
<?php namespace App\Repositories; use IlluminateSupportSeriveProvider; class BackSerivePrivider extends ServiceProvider { public function register() { $this->app->bind('App\Repositories\HouseRepositoryInterface', 'App\Repositories\DbHouseRepository'); } }
<code><span><span> </span></span></code>
Of course, you can also create a new folder to mainly store our provider
related files.
The above code mainly says that when you use type hints HouseRepositoryInterface
in the controller
layer, we know that you will use DbHouseRepository
.
Providers Array
actually above In the code, we have implemented a dependency injection, but if we want to use it, we need to write it manually. In order to be more convenient, we need to add this providers
to providers in app/config/app.php
In the array, just add AppRepositoriesBackendServiceProvider::class,
controller
After we complete the above content, we only need to simple in Controller
The calling method replaces the previous complex database call, as shown below: HousesController.php
<?php use App\repositories\HouseRepositoryInterface; class HousesController extends BaseController { public function __construct(HouseRepositoryInterface $house) { $this->house = $house; } public function index() { $houses = $this->house->selectAll(); return View::make('houses.index', compact('houses')); } public function create() { return View::make('houses.create'); } public function show($id) { $house = $this->house->find($id); return View::make('houses.show', compact('house')); } }
In this way, the conversion of the entire model is completed
The above has introduced the Laravel Repository mode, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.