Laravel Repository Pattern

WBOY
Release: 2016-07-30 13:30:50
Original
946 people have browsed it

Repository Pattern

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 special 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 easier to read. 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 Putting yourself into a blind spot, for example, not choosing to use Repositories will make your application testing very difficult, (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'));
        }
    }
Copy after login

This is a very 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 of. 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.

Using repositories

It actually requires quite a few steps to complete the entire repository pattern, but once you complete it a few times it will naturally become a habit. Below we will introduce each step in detail.

1. Create the Repository folder

First we need to create our own Repository folderrepositories in the app 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 related methods that our repository class must implement, as shown in the example below, where the namespace is emphasized again Be sure to remember to add it.
HouseRepositoryInterface.php

<?php namespace App\Repositories;

interface HouseRepositoryInterface {
    public function selectAll();
    
    public function find($id);
}
Copy after login

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 Most database queries are put in there, no matter how complex. As in the example below
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);
    }
}
Copy after login

<code><span><span> </span></span></code>
Copy after login

4: Create a backend service provider

First you need to understand the so-called service provider, 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');
    }
}
Copy after login
<code><span><span> </span></span></code>
Copy after login

Of course, you can also create a new folder to mainly store our provider related files.
The above code mainly says that when you use type hints HouseRepositoryInterface in the controller layer, we know that you will use DbHouseRepository.

5: Update your Providers Array

actually above In the code, we have implemented a dependency injection, but if we want to use it, we need to write it manually. In order to be more convenient, we need to add this providers to providers in app/config/app.php In the array, just add AppRepositoriesBackendServiceProvider::class,

6 at the end: Finally, use dependency injection to update your controller

After we complete the above content, we only need to simple in Controller The calling method replaces the previous complex database call, as shown below:
HousesController.php

<?php 

use App\repositories\HouseRepositoryInterface;

class HousesController extends BaseController {

    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'));

    }
}
Copy after login

In this way, the conversion of the entire model is completed

The above has introduced the Laravel Repository mode, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template