LaravelRepository mode, laravelrepository_PHP tutorial

Source: Internet
Author: User
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 PatternIs very useful. In fact, we don't have to simply use this particular design pattern.LaravelHowever, in the following scenarios, we will useOOPFrameworkLaravelDemonstrate how to userepositoriesMake ourControllerThe layer is no longer so cool, more decoupled and easy to read. Let's take a deeper look.

Not used repositories

Actually usedRepositoriesIt 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 useRepositoriesIt 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.EloquentThis code works normally when interacting with the database,controllerForEloquentIt will be tightly coupled. Here we can injectrepositoryCreate a code version of the decoupling type. this decoupled version code can simplify the implementation of subsequent programs.

Userepositories

Actually complete the wholerepositoryThe 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. CreateRepositoryFolder

First, we needappCreate your own folderRepository FolderrepositoriesAnd then set the corresponding namespace for each file in the folder.

2: create the correspondingInterfaceClass

Step 2: create the corresponding interface, which determines ourrepositoryThe relevant methods that must be implemented by the class are shown in the following example.
HouseRepositoryInterface.php

  

  

3: Create the correspondingRepositoryClass

Now we can createrepositoryClass 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 provide

First, 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 storeproviderRelated files.
The above code mainly says that when youcontrollerLayer 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. phpprovidersIn the array, you only need to addApp\Repositories\BackendServiceProvider::class,

6: Use dependency injection to update yourcontroller

After we finish the above contentControllerYou 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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.