亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

如何在Laravel5.8中正確地應用Repository設計模式

 更新時間:2019年11月26日 09:43:06   投稿:zx  
這篇文章主要介紹了如何在Laravel5.8中正確地應用Repository設計模式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

在本文中,我會向你展示如何在 Laravel 中從頭開始實現(xiàn) repository 設計模式。我將使用 Laravel 5.8.3 版,但 Laravel 版本不是最重要的。在開始寫代碼之前,你需要了解一些關于 repository 設計模式的相關信息。

repository 設計模式允許你使用對象,而不需要了解這些對象是如何持久化的。本質(zhì)上,它是數(shù)據(jù)層的抽象。

這意味著你的業(yè)務邏輯不需要了解如何檢索數(shù)據(jù)或數(shù)據(jù)源是什么,業(yè)務邏輯依賴于 repository 來檢索正確的數(shù)據(jù)。

關于這個模式,我看到有人將它誤解為 repository 被用來創(chuàng)建或更新數(shù)據(jù)。 這不是 repository 應該做的,repository 不應該創(chuàng)建或更新數(shù)據(jù),僅僅用于檢索數(shù)據(jù)。

理解透了吧?接下來一起寫代碼

既然我們從頭開始,那么我們先創(chuàng)建一個新的 Laravel 項目吧:

composer create-project --prefer-dist laravel/laravel repository

對于本教程,我們將構建一個小型的博客應用?,F(xiàn)在我們已經(jīng)創(chuàng)建好了一個新的 Laravel 項目,接下來應該為它創(chuàng)建一個控制器和模型。

php artisan make:controller BlogController

這將在 app/Http/Controllers 目錄中創(chuàng)建 BlogController 。

php artisan make:model Models/Blog -m

提示:

-m 選項會創(chuàng)建一個對應的數(shù)據(jù)庫遷移,你可以在 database/migrations 目錄中找到所生成的遷移。

現(xiàn)在你應該能在 app/Models 目錄中找到剛生成的模型 Blog 了吧。這只是一種我喜歡的存放模型的方式。

現(xiàn)在我們有了控制器和模型,是時候看看我們創(chuàng)建的遷移文件了。除了默認的 Laravel 時間戳字段外,我們的博客只需要 標題、內(nèi)容 和 用戶 ID 字段。

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateBlogsTable extends Migration
{
 public function up()
 {
  Schema::create('blogs', function (Blueprint $table) {
   $table->bigIncrements('id');
   $table->string('title');
   $table->text('content');
   $table->integer('user_id');
   $table->timestamps();

   $table->foreign('user_id')
     ->references('id')
     ->on('users');
  });
 }

 public function down()
 {
  Schema::dropIfExists('blogs');
 }
}

提示:

如果你使用的是 Laravel 5.8 以下的舊版本,請將

$table->bigIncrements('id');

替換為:

$table->increments('id');

設置數(shù)據(jù)庫

我將使用 MySQL 數(shù)據(jù)庫作為示例,第一步就是創(chuàng)建一個新的數(shù)據(jù)庫。

mysql -u root -p 
create database laravel_repository;

以上命令將會創(chuàng)建一個叫 laravel_repository 的新數(shù)據(jù)庫。接下來我們需要添加數(shù)據(jù)庫信息到 Laravel 根目錄的 .env 文件中。

DB_DATABASE=laravel_repository
DB_USERNAME=root
DB_PASSWORD=secret

當你更新了 .env 文件后我們需要清空緩存:

php artisan config:clear

運行遷移

現(xiàn)在我們已經(jīng)設置好了數(shù)據(jù)庫,可以開始運行遷移了:

php artisan migrate

這將會創(chuàng)建 blogs 表,包含了我們在遷移中聲明的 title , content 和 user_id 字段。

實現(xiàn) repository 設計模式

一切就緒,我們現(xiàn)在可以開始實現(xiàn) repository 設計風格了。我們將會在 app 目錄中創(chuàng)建 Repositories 目錄。我們將要創(chuàng)建的第二個目錄是 Interfaces 目錄,這個目錄位于 Repositories 目錄中。

在 Interfaces 文件中我們將創(chuàng)建一個包含兩個方法的 BlogRepositoryInterface 接口。

  1. 返回所有博客文章的 all 方法
  2. 返回特定用戶所有博客文章的 getByUser 方法
<?php

namespace App\Repositories\Interfaces;

use App\User;

interface BlogRepositoryInterface
{
 public function all();

 public function getByUser(User $user);
}

我們需要創(chuàng)建的最后一個類是將要實現(xiàn) BlogRepositoryInterface 的 BlogRepository ,我們會寫一個最簡單的實現(xiàn)方式。

<?php

namespace App\Repositories;

use App\Models\Blog;
use App\User;
use App\Repositories\Interfaces\BlogRepositoryInterface;

class BlogRepository implements BlogRepositoryInterface
{
 public function all()
 {
  return Blog::all();
 }

 public function getByUser(User $user)
 {
  return Blog::where('user_id'. $user->id)->get();
 }
}

你的 Repositories 目錄應該像這樣:

app/
└── Repositories/
 ├── BlogRepository.php
 └── Interfaces/
  └── BlogRepositoryInterface.php

你現(xiàn)在已經(jīng)成功創(chuàng)建了一個 repository 了。但是我們還沒有完成,是時候開始使用我們的 repository 了。

在控制器中使用 Repository

要開始使用 BlogRepository ,我們首先需要將其注入到 BlogController 。由于 Laravel 的依賴注入,我們很容易用另一個來替換它。這就是我們控制器的樣子:

<?php

namespace App\Http\Controllers;

use App\Repositories\Interfaces\BlogRepositoryInterface;
use App\User;

class BlogController extends Controller
{
 private $blogRepository;

 public function __construct(BlogRepositoryInterface $blogRepository)
 {
  $this->blogRepository = $blogRepository;
 }

 public function index()
 {
  $blogs = $this->blogRepository->all();

  return view('blog')->withBlogs($blogs);
 }

 public function detail($id)
 {
  $user = User::find($id);
  $blogs = $this->blogRepository->getByUser($user);

  return view('blog')->withBlogs($blogs);
 }
}

如你所見,控制器中的代碼很簡短,可讀性非常的高。不需要十行代碼就可以獲取到所需的數(shù)據(jù),多虧了 repository ,所有這些邏輯都可以在一行代碼中完成。這對單元測試也很好,因為 repository 的方法很容易復用。

repository 設計模式也使更改數(shù)據(jù)源變得更加容易。在這個例子中,我們使用 MySQL 數(shù)據(jù)庫來檢索我們的博客內(nèi)容。我們使用 Eloquent 來完成查詢數(shù)據(jù)庫操作。但是假設我們在某個網(wǎng)站上看到了一個很棒的博客 API,我們想使用這個 API 作為數(shù)據(jù)源,我們所要做的就是重寫 BlogRepository 來調(diào)用這個 API 替換 Eloquent 。

RepositoryServiceProvider

我們將注入 BlogController 中的 BlogRepository ,而不是注入 BlogController 中的 BlogRepositoryInterface ,然后讓服務容器決定將使用哪個存儲庫。這將在 AppServiceProvider 的 boot 方法中實現(xiàn),但我更喜歡為此創(chuàng)建一個新的 provider 來保持整潔。

php artisan make:provider RepositoryServiceProvider

我們?yōu)榇藙?chuàng)建一個新的 provider 的原因是,當您的項目開始發(fā)展為大型項目時,結構會變得非常凌亂。設想一下,一個擁有 10 個以上模型的項目,每個模型都有自己的 repository ,你的 AppServiceProvider 可讀性將會大大降低。

我們的 RepositoryServiceProvider 會像下面這樣:

<?php

namespace App\Providers;

use App\Repositories\BlogRepository;
use App\Repositories\Interfaces\BlogRepositoryInterface;
use Illuminate\Support\ServiceProvider;

class RepositoryServiceProvider extends ServiceProvider
{
 public function register()
 {
  $this->app->bind(
   BlogRepositoryInterface::class, 
   BlogRepository::class
  );
 }
}

留意用另一個 repository 替代 BlogRepository 是多么容易!

不要忘記添加 RepositoryServiceProvider 到 config/app.php 文件的 providers 列表中。完成了這些后我們需要清空緩存:

php artisan config:clear

就是這樣

現(xiàn)在你已經(jīng)成功實現(xiàn)了 repository 設計模式,不是很難吧?

你可以選擇增加一些路由和視圖來拓展代碼,但本文將在這里結束,因為本文主要是介紹 repository 設計模式的。

原文地址:https://itnext.io/repository-design-pattern-done-right-in-laravel-d177b5fa75d4

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關文章

  • PHP設計模式(四)原型模式Prototype實例詳解【創(chuàng)建型】

    PHP設計模式(四)原型模式Prototype實例詳解【創(chuàng)建型】

    這篇文章主要介紹了PHP設計模式:原型模式Prototype,結合實例形式詳細分析了PHP原型模式Prototype的基本概念、功能、原理、實現(xiàn)方法與操作注意事項,需要的朋友可以參考下
    2020-05-05
  • PHP用身份證號獲取星座和生肖的方法

    PHP用身份證號獲取星座和生肖的方法

    PHP用身份證號獲取星座和生肖,實用的PHP函數(shù)
    2013-11-11
  • php-7.3.6 編譯安裝過程

    php-7.3.6 編譯安裝過程

    這篇文章主要介紹了php-7.3.6 編譯安裝過程,本文通過實例文字相結合給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • docker-compose部署php項目實例詳解

    docker-compose部署php項目實例詳解

    在本篇文章里小編給大家整理了關于docker-compose部署php項目的相關實例以及代碼內(nèi)容,有需要的朋友們可以學習參考下。
    2019-07-07
  • excel的導出和下載(實例講解)

    excel的導出和下載(實例講解)

    下面小編就為大家?guī)硪黄猠xcel的導出和下載(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • thinkphp模板賦值與替換實例簡述

    thinkphp模板賦值與替換實例簡述

    這篇文章主要介紹了thinkphp模板賦值與替換的方法,以實例形式簡述了模板復制的兩種簡單方法,以及模板替換中的特殊字符串說明與用法實例,是使用頻率比較高的技巧,需要的朋友可以參考下
    2014-11-11
  • 使用Thinkphp框架開發(fā)移動端接口

    使用Thinkphp框架開發(fā)移動端接口

    本文給大家分享的是使用thinkphp框架開發(fā)移動端接口的2種方法,一種是開發(fā)API,另外一種是實現(xiàn)移動端訪問自動切換移動主題模板,從而實現(xiàn)偽app訪問,下面我們就來詳細看下如何實現(xiàn)吧。
    2015-08-08
  • 解決laravel5.4下的group by報錯的問題

    解決laravel5.4下的group by報錯的問題

    今天小編就為大家分享一篇解決laravel5.4下的group by報錯的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10
  • CI框架驗證碼CAPTCHA輔助函數(shù)用法實例

    CI框架驗證碼CAPTCHA輔助函數(shù)用法實例

    這篇文章主要介紹了CI框架驗證碼CAPTCHA輔助函數(shù)用法,以實例形式詳細講述了基于CAPTCHA輔助函數(shù)實現(xiàn)驗證碼的方法,需要的朋友可以參考下
    2014-11-11
  • PHP實現(xiàn)QQ登錄的開原理和實現(xiàn)過程

    PHP實現(xiàn)QQ登錄的開原理和實現(xiàn)過程

    本文主要給大家講述了PHP實現(xiàn)QQ登錄的開原理以及相關的代碼實現(xiàn)過程,對此有興趣和需要的朋友參考下吧。
    2018-02-02

最新評論