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 particular 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 more readable. Let’s take a closer look.
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 get yourself into a dead end. For example, if you don't choose to use Repositories
, it will be very difficult to test your application. (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 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
. 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.
repositories
Actually completing the entire repository
mode requires quite a few steps, 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 app
folderRepository
in the repositories
folder, and then each file in the folder must set the corresponding namespace.
Interface
classThe second step is to create the corresponding interface, which determines the relevant methods that our repository
class must implement, as shown in the example below. It is emphasized again that the namespace must be added. 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 database queries, no matter how complex. As in the following example DbHouseRepository.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 class="php"><span class="hljs-preprocessor"> </span></span></code>
First you need to understand the so-called service provision, please refer to the manual service provider BackendServiceProvider.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 class="php"><span class="hljs-preprocessor"> </span></span></code>
Of course, you can also create a new folder to mainly store our provider
related files.
What the above code mainly says is that when you use type hints controller
in the HouseRepositoryInterface
layer, we know that you will use DbHouseRepository
.
Providers Array
In fact, in the above code, we have implemented a dependency injection, but if we want to use it here, we need to write it manually. For more convenience, we need to add this providers
to app/config/ In the providers
array in app.php, just add AppRepositoriesBackendServiceProvider::class,
controller
After we complete the above content, we only need to simply call the method in Controller
instead of the previous complex database call, as shown below: HousesController.php
<?php use App\repositories\HouseRepositoryInterface; class HousesController extends BaseController { protected $house; 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 entire mode conversion is completed