Home  >  Article  >  Backend Development  >  Laravel Repository pattern, laravelrepository_PHP tutorial

Laravel Repository pattern, laravelrepository_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:44:47978browse

Laravel 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 ControllerLayers 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 appfolderRepository  in the repositories folder, and then each file in the folder must set the corresponding namespace.

2: Create the corresponding Interfaceclass

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,

at the end

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1048733.htmlTechArticleLaravel Repository pattern, laravelrepository Repository pattern In order to keep the code clean and readable, it is very useful to use the Repository Pattern of. In fact, we don’t have to...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn