Laravel Repository pattern, laravelrepository_PHP tutorial
Jul 13, 2016 am 09:44 AMLaravel Repository mode, laravelrepository
Repository mode
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.
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 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.
Use 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.
1. Create 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.
2: Create the corresponding Interface
class
The 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); }
3: Create the corresponding Repository
class
Now 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>
4: Create backend service provider
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
.
5: Update your 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,
6: Finally update your controller
using dependency injection
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

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use sql statement to query the storage structure of mysql database

How to export the queried data in navicat

How to solve mysql database initialization failure

How to execute sql statement in mysql database

MySQL transaction processing: the difference between automatic submission and manual submission

Advantages and limitations of MySQL views
