這篇“Laravel5.8中應(yīng)用Repository設(shè)計(jì)模式的方法”除了程序員外大部分人都不太理解,今天小編為了讓大家更加理解“Laravel5.8中應(yīng)用Repository設(shè)計(jì)模式的方法”,給大家總結(jié)了以下內(nèi)容,具有一定借鑒價(jià)值,內(nèi)容詳細(xì)步驟清晰,細(xì)節(jié)處理妥當(dāng),希望大家通過(guò)這篇文章有所收獲,下面讓我們一起來(lái)看看具體內(nèi)容吧。

Laravel 是一套簡(jiǎn)潔、優(yōu)雅的PHP Web開發(fā)框架。它可以讓你從面條一樣雜亂的代碼中解脫出來(lái);它可以幫你構(gòu)建一個(gè)完美的網(wǎng)絡(luò)APP,而且每行代碼都可以簡(jiǎn)潔、富于表達(dá)力。
在本文中,我會(huì)向你展示如何在 Laravel 中從頭開始實(shí)現(xiàn)repository 設(shè)計(jì)模式。我將使用 Laravel 5.8.3 版,但 Laravel 版本不是最重要的。在開始寫代碼之前,你需要了解一些關(guān)于repository 設(shè)計(jì)模式的相關(guān)信息。

repository 設(shè)計(jì)模式允許你使用對(duì)象,而不需要了解這些對(duì)象是如何持久化的。本質(zhì)上,它是數(shù)據(jù)層的抽象。
這意味著你的業(yè)務(wù)邏輯不需要了解如何檢索數(shù)據(jù)或數(shù)據(jù)源是什么,業(yè)務(wù)邏輯依賴于repository 來(lái)檢索正確的數(shù)據(jù)。
關(guān)于這個(gè)模式,我看到有人將它誤解為repository 被用來(lái)創(chuàng)建或更新數(shù)據(jù)。 這不是repository 應(yīng)該做的,repository 不應(yīng)該創(chuàng)建或更新數(shù)據(jù),僅僅用于檢索數(shù)據(jù)。
既然我們從頭開始,那么我們先創(chuàng)建一個(gè)新的 Laravel 項(xiàng)目吧:
composer create-project --prefer-dist laravel/laravel repository
對(duì)于本教程,我們將構(gòu)建一個(gè)小型的博客應(yīng)用。現(xiàn)在我們已經(jīng)創(chuàng)建好了一個(gè)新的 Laravel 項(xiàng)目,接下來(lái)應(yīng)該為它創(chuàng)建一個(gè)控制器和模型。
php artisan make:controller BlogController
這將在app/Http/Controllers 目錄中創(chuàng)建BlogController 。
php artisan make:model Models/Blog -m
提示:-m 選項(xiàng)會(huì)創(chuàng)建一個(gè)對(duì)應(yīng)的數(shù)據(jù)庫(kù)遷移,你可以在 *database/migrations 目錄中找到所生成的遷移。*
現(xiàn)在你應(yīng)該能在app/Models 目錄中找到剛生成的模型Blog 了吧。這只是一種我喜歡的存放模型的方式。
現(xiàn)在我們有了控制器和模型,是時(shí)候看看我們創(chuàng)建的遷移文件了。除了默認(rèn)的 Laravel 時(shí)間戳字段外,我們的博客只需要標(biāo)題、內(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 以下的舊版本,請(qǐng)將
$table->bigIncrements('id');替換為:
$table->increments('id');我將使用MySQL 數(shù)據(jù)庫(kù)作為示例,第一步就是創(chuàng)建一個(gè)新的數(shù)據(jù)庫(kù)。
mysql -u root -p create database laravel_repository;
以上命令將會(huì)創(chuàng)建一個(gè)叫laravel_repository 的新數(shù)據(jù)庫(kù)。接下來(lái)我們需要添加數(shù)據(jù)庫(kù)信息到 Laravel 根目錄的.env 文件中。
DB_DATABASE=laravel_repositoryDB_USERNAME=rootDB_PASSWORD=secret
當(dāng)你更新了.env 文件后我們需要清空緩存:
php artisan config:clear
現(xiàn)在我們已經(jīng)設(shè)置好了數(shù)據(jù)庫(kù),可以開始運(yùn)行遷移了:
php artisan migrate
這將會(huì)創(chuàng)建blogs 表,包含了我們?cè)谶w移中聲明的title ,content 和user_id 字段。
一切就緒,我們現(xiàn)在可以開始實(shí)現(xiàn)repository 設(shè)計(jì)風(fēng)格了。我們將會(huì)在app 目錄中創(chuàng)建Repositories 目錄。我們將要?jiǎng)?chuàng)建的第二個(gè)目錄是Interfaces 目錄,這個(gè)目錄位于Repositories 目錄中。
在Interfaces 文件中我們將創(chuàng)建一個(gè)包含兩個(gè)方法的BlogRepositoryInterface 接口。
返回所有博客文章的all 方法
返回特定用戶所有博客文章的getByUser 方法
<?php
namespace App\Repositories\Interfaces;use App\User;interface BlogRepositoryInterface{
public function all();
public function getByUser(User $user);}我們需要?jiǎng)?chuàng)建的最后一個(gè)類是將要實(shí)現(xiàn)BlogRepositoryInterface 的BlogRepository ,我們會(huì)寫一個(gè)最簡(jiǎn)單的實(shí)現(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 目錄應(yīng)該像這樣:
app/└── Repositories/ ├── BlogRepository.php └── Interfaces/ └── BlogRepositoryInterface.php
你現(xiàn)在已經(jīng)成功創(chuàng)建了一個(gè)repository 了。但是我們還沒有完成,是時(shí)候開始使用我們的repository 了。
要開始使用BlogRepository ,我們首先需要將其注入到BlogController 。由于 Laravel 的依賴注入,我們很容易用另一個(gè)來(lái)替換它。這就是我們控制器的樣子:
<?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);
}}如你所見,控制器中的代碼很簡(jiǎn)短,可讀性非常的高。不需要十行代碼就可以獲取到所需的數(shù)據(jù),多虧了repository ,所有這些邏輯都可以在一行代碼中完成。這對(duì)單元測(cè)試也很好,因?yàn)?em>repository 的方法很容易復(fù)用。
repository 設(shè)計(jì)模式也使更改數(shù)據(jù)源變得更加容易。在這個(gè)例子中,我們使用MySQL 數(shù)據(jù)庫(kù)來(lái)檢索我們的博客內(nèi)容。我們使用Eloquent 來(lái)完成查詢數(shù)據(jù)庫(kù)操作。但是假設(shè)我們?cè)谀硞€(gè)網(wǎng)站上看到了一個(gè)很棒的博客 API,我們想使用這個(gè) API 作為數(shù)據(jù)源,我們所要做的就是重寫BlogRepository 來(lái)調(diào)用這個(gè) API 替換Eloquent 。
我們將注入BlogController 中的BlogRepository ,而不是注入BlogController 中的BlogRepositoryInterface ,然后讓服務(wù)容器決定將使用哪個(gè)存儲(chǔ)庫(kù)。這將在AppServiceProvider 的boot 方法中實(shí)現(xiàn),但我更喜歡為此創(chuàng)建一個(gè)新的provider 來(lái)保持整潔。
php artisan make:provider RepositoryServiceProvider
我們?yōu)榇藙?chuàng)建一個(gè)新的provider 的原因是,當(dāng)您的項(xiàng)目開始發(fā)展為大型項(xiàng)目時(shí),結(jié)構(gòu)會(huì)變得非常凌亂。設(shè)想一下,一個(gè)擁有 10 個(gè)以上模型的項(xiàng)目,每個(gè)模型都有自己的repository ,你的AppServiceProvider 可讀性將會(huì)大大降低。
我們的RepositoryServiceProvider 會(huì)像下面這樣:
<?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
);
}}留意用另一個(gè)repository 替代BlogRepository 是多么容易!
不要忘記添加RepositoryServiceProvider 到config/app.php 文件的providers 列表中。完成了這些后我們需要清空緩存:
'providers' => [ //測(cè)試¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥ \App\Providers\RepositoryServiceProvider::class],
php artisan config:clear
感謝你的閱讀,希望你對(duì)“Laravel5.8中應(yīng)用Repository設(shè)計(jì)模式的方法”這一關(guān)鍵問題有了一定的理解,具體使用情況還需要大家自己動(dòng)手實(shí)驗(yàn)使用過(guò)才能領(lǐng)會(huì),快去試試吧,如果想閱讀更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當(dāng)前題目:Laravel5.8中應(yīng)用Repository設(shè)計(jì)模式的方法-創(chuàng)新互聯(lián)
鏈接地址:http://chinadenli.net/article4/hhpoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站營(yíng)銷、網(wǎng)站策劃、網(wǎng)站改版、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容