Repository mode
In order to maintain the cleanliness and readability of the code, use Repository Pattern
is very useful. In fact, we don't have to use this particular design pattern just to Laravel
be used, but in the scenario below, we'll use OOP
the framework Laravel
to show how to use it to repositories
make our Controller
layers less verbose, more decoupled and easier to read. Let's take a deeper look at the following.
Do not userepositories
In fact, the use Repositories
is not necessary, in your application you can completely do not use this design mode under the premise of the majority of things, but over time you may put yourself into a corner, such as not to use Repositories
will make your application testing is not easy, (swapping out implementations) The specific implementation will become very complex, let's look at an example.
HousesController.php
This is a very typical code use Eloquent
and database interaction, this code work is normal, but the controller
layer Eloquent
will be tightly coupled. Here we can inject a repository
version of the code that creates a decoupling type, and this decoupled version code can make the implementation of the subsequent program simpler.
Userepositories
In fact, repository
it takes a lot of steps to complete the pattern, but once you've done it a few times it will naturally become a habit, and we'll cover each step in detail below.
1. Create a Repository
folder
First we need to create our own folder in the folder app
Repository
repositories
, and then each file in the folder to set the appropriate namespace.
2: Create the appropriate Interface
class
The second step is to create the corresponding interface, which determines the repository
relevant methods that our class must implement, as shown in the following example, where it is important to remember that namespaces must be added.
HouseRepositoryInterface.php
3: Create the corresponding Repository
class
Now we can create our repository
class to work for us, in which we can put most of our database queries, no matter how complex. As the following example
DbHouseRepository.php
4: Create back-end service delivery
First you need to understand the so-called service delivery, please refer to the manual service provider
BackendServiceProvider.php
App->bind (' App\repositories\houserepositoryinterface ', ' app\repositories\dbhouserepository ');} }
Of course, you can also create a new folder to focus on our provider
related files.
The above section of code mainly says that when you controller
use type hints on the layer HouseRepositoryInterface
, we know you will be using DbHouseRepository
.
5: Update yourProviders Array
In fact, in the above code, we have implemented a dependency injection, but if we want to use this we are need to write manually, in order to more aspects, we need providers
to add this to app/config/app.php in the providers
array inside, only need to add in the lastApp\Repositories\BackendServiceProvider::class,
6: Finally use Dependency injection to update yourcontroller
After we have done that, we need Controller
to simply call the method instead of the complex database call, as in the following:
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 '));} }
So the transformation of the whole pattern is done.
The above describes the Laravel Repository model, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.