LaravelRepository mode, laravelrepository. In LaravelRepository mode and laravelrepositoryRepository mode, RepositoryPattern is very useful to keep the code clean and readable. In fact, we do not need to use the Laravel Repository mode.
Repository mode
To keep the code concise and readable, useRepository Pattern
Is very useful. In fact, we don't have to simply use this particular design pattern.Laravel
However, in the following scenarios, we will useOOP
FrameworkLaravel
Demonstrate how to userepositories
Make ourController
The layer is no longer so cool, more decoupled and easy to read. Let's take a deeper look.
Not used
repositories
Actually usedRepositories
It is not necessary. in your application, you can do the vast majority of things without using this design pattern. However, over time, you may put yourself in a dead corner, for example, do not select to useRepositories
It will make the test of your application very difficult, and the implementation of (swapping out implementations) will become very complicated. let's look at an example below.
HousesController.php
This is a typical piece of code.Eloquent
This code works normally when interacting with the database,controller
ForEloquent
It will be tightly coupled. Here we can injectrepository
Create a code version of the decoupling type. this decoupled version code can simplify the implementation of subsequent programs.
Userepositories
Actually complete the wholerepository
The mode requires a lot of steps, but once you finish several times, it will naturally become a habit. next we will introduce each step in detail.
1. CreateRepository
FolderFirst, we needapp
Create your own folderRepository
Folderrepositories
And then set the corresponding namespace for each file in the folder.
2: create the correspondingInterface
ClassStep 2: create the corresponding interface, which determines ourrepository
The relevant methods that must be implemented by the class are shown in the following example.
HouseRepositoryInterface.php
3: Create the correspondingRepository
ClassNow we can createrepository
Class to work for us, in this class file we can put the vast majority of our database queries, no matter how complicated. Example
DbHouseRepository.php
4: Create a backend service to provideFirst, you need to understand the so-called service provision. please refer to the manual service provider
BackendServiceProvider.php
app->bind('App\Repositories\HouseRepositoryInterface', 'App\Repositories\DbHouseRepository'); }}
Of course, you can also create a folder to storeprovider
Related files.
The above code mainly says that when youcontroller
Layer usage type promptHouseRepositoryInterface
, We know that you will useDbHouseRepository
.
5: Update yourProviders Array
In fact, in the above code, we have implemented a dependency injection, but if we want to use it here, we need to manually write it. For more information, we need to add thisproviders
To app/config/app. phpproviders
In the array, you only need to addApp\Repositories\BackendServiceProvider::class,
6: Use dependency injection to update yourcontroller
After we finish the above contentController
You only need a simple call method to replace the previous complex database call, as shown in the following content:
HousesController.php
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 is converted.
Http://www.bkjia.com/PHPjc/1048733.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1048733.htmlTechArticleLaravel Repository Pattern, laravelrepository Repository Pattern is very useful to keep the code clean and readable. In fact, we don't have...