Table of Contents
Laravel Repository mode, laravelrepository
Repository mode
Use repositories
1. Create Repository folder
2: Create the corresponding Interfaceclass
3: Create the corresponding Repository class
4: Create backend service provider
5: Update your Providers Array
6: Finally update your controller
Home Backend Development PHP Tutorial Laravel Repository pattern, laravelrepository_PHP tutorial

Laravel Repository pattern, laravelrepository_PHP tutorial

Jul 13, 2016 am 09:44 AM
sql statement

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'));
        }
    }
Copy after login

 

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);
}
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 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);
    }
}
Copy after login

 

<code><span class="php"><span class="hljs-preprocessor"> </span></span></code>
Copy after login
Copy after login

4: Create backend service provider

First you need to understand the so-called service provision, please refer to the manual service provider
BackendServiceProvider.php

&lt;?php namespace App\Repositories;

use IlluminateSupportSeriveProvider;

class BackSerivePrivider extends ServiceProvider {

    public function register()
    {
        $this-&gt;app-&gt;bind('App\Repositories\HouseRepositoryInterface', 'App\Repositories\DbHouseRepository');
    }
}
Copy after login

 

<code><span class="php"><span class="hljs-preprocessor"> </span></span></code>
Copy after login
Copy after login

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

&lt;?php 

use App\repositories\HouseRepositoryInterface;

class HousesController extends BaseController {
    protected $house;
    
    public function __construct(HouseRepositoryInterface $house)
    {
        $this-&gt;house = $house;
    }


    public function index()
    {
        $houses = $this-&gt;house-&gt;selectAll();

        return View::make('houses.index', compact('houses'));
        
    }


    public function create()
    {
        return View::make('houses.create');
    }


    public function show($id)
    {
        $house = $this-&gt;house-&gt;find($id);
        
        return View::make('houses.show', compact('house'));

    }
}
Copy after login

 

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 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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use the iif function in excel How to use the iif function in excel Mar 20, 2024 pm 06:10 PM

How to use the iif function in excel

How to query oracle database logs How to query oracle database logs Apr 07, 2024 pm 04:51 PM

How to query oracle database logs

How to use sql statement to query the storage structure of mysql database How to use sql statement to query the storage structure of mysql database Apr 14, 2024 pm 07:45 PM

How to use sql statement to query the storage structure of mysql database

How to export the queried data in navicat How to export the queried data in navicat Apr 24, 2024 am 04:15 AM

How to export the queried data in navicat

How to solve mysql database initialization failure How to solve mysql database initialization failure Apr 14, 2024 pm 07:12 PM

How to solve mysql database initialization failure

How to execute sql statement in mysql database How to execute sql statement in mysql database Apr 14, 2024 pm 07:48 PM

How to execute sql statement in mysql database

MySQL transaction processing: the difference between automatic submission and manual submission MySQL transaction processing: the difference between automatic submission and manual submission Mar 16, 2024 am 11:33 AM

MySQL transaction processing: the difference between automatic submission and manual submission

Advantages and limitations of MySQL views Advantages and limitations of MySQL views Mar 15, 2024 pm 09:09 PM

Advantages and limitations of MySQL views

See all articles