為了保持代碼的整潔性和可讀性,使用Repository Pattern是非常有用的。事實(shí)上,我們也不必僅僅為了使用這個(gè)特別的設(shè)計(jì)模式去使用Laravel,然而在下面的場(chǎng)景下,我們將使用OOP的框架Laravel去展示如何使用repositories使我們的Controller層不再那么啰嗦、更加解耦和易讀。下面讓我們更深入的研究一下。
repositories其實(shí)使用Repositories并不是必要的,在你的應(yīng)用中你完全可以不使用這個(gè)設(shè)計(jì)模式的前提下完成絕大多數(shù)的事情,然而隨著時(shí)間的推移你可能把自己陷入一個(gè)死角,比如不選擇使用Repositories會(huì)使你的應(yīng)用測(cè)試很不容易,(swapping out implementations)具體的實(shí)現(xiàn)將會(huì)變的很復(fù)雜,下面我們看一個(gè)例子。HousesController.php
<?php html' target='_blank'>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')); } }
這是一個(gè)很典型的一段代碼使用Eloquent和數(shù)據(jù)庫(kù)交互,這段代碼工作的很正常,但是controller層對(duì)于Eloquent而言將是緊耦合的。在此我們可以注入一個(gè)repository創(chuàng)建一個(gè)解耦類型的代碼版本,這個(gè)解耦的版本代碼可以使后續(xù)程序的具體實(shí)現(xiàn)更加簡(jiǎn)單。
repositories其實(shí)完成整個(gè)repository模式需要相當(dāng)多的步驟,但是一旦你完成幾次就會(huì)自然而然變成了一種習(xí)慣了,下面我們將詳細(xì)介紹每一步。
Repository文件夾首先我們需要在app文件夾創(chuàng)建自己Repository文件夾repositories,然后文件夾的每一個(gè)文件都要設(shè)置相應(yīng)的命名空間。
Interface類第二步創(chuàng)建對(duì)應(yīng)的接口,其決定著我們的repository類必須要實(shí)現(xiàn)的相關(guān)方法,如下例所示,在此再次強(qiáng)調(diào)的是命名空間一定要記得加上。HouseRepositoryInterface.php
<?php namespace AppRepositories;interface HouseRepositoryInterface { public function selectAll(); public function find($id);}3:創(chuàng)建對(duì)應(yīng)的
Repository類現(xiàn)在我們可以創(chuàng)建我們repository類 來(lái)給我們干活了,在這個(gè)類文件中我們可以把我們的絕大多數(shù)的數(shù)據(jù)庫(kù)查詢都放進(jìn)去,不論多么復(fù)雜。如下面的例子DbHouseRepository.php
<?php namespace AppRepositories;use House;class DbHouseRepository implements HouseRepositoryInterface { public function selectAll() { return House::all(); } public function find($id) { return House::find($id); }}
4:創(chuàng)建后端服務(wù)提供首先你需要理解所謂服務(wù)提供,請(qǐng)參考手冊(cè)服務(wù)提供者BackendServiceProvider.php
<?php namespace AppRepositories;use IlluminateSupportSeriveProvider;class BackSerivePrivider extends ServiceProvider { public function register() { $this->app->bind('AppRepositoriesHouseRepositoryInterface', 'AppRepositoriesDbHouseRepository'); }}
當(dāng)然你也可以新建一個(gè)文件夾主要放我們的provider相關(guān)文件。
上面一段代碼主要說的是,當(dāng)你在controller層使用類型提示HouseRepositoryInterface,我們知道你將會(huì)使用DbHouseRepository.
Providers Array其實(shí)在上面的代碼中,我們已經(jīng)實(shí)現(xiàn)了一個(gè)依賴注入,但如果我們要使用在此我們是需要手動(dòng)去寫的,為了更為方面,我們需要增加這個(gè)providers到app/config/app.php 中的providers數(shù)組里面,只需要在最后加上AppRepositoriesBackendServiceProvider::class,
controller當(dāng)我們完成上面的那些內(nèi)容之后,我們?cè)?code>Controller只需要簡(jiǎn)單的調(diào)用方法代替之前的復(fù)雜的數(shù)據(jù)庫(kù)調(diào)用,如下面內(nèi)容:HousesController.php
<?php use AppepositoriesHouseRepositoryInterface;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')); }}這樣 整個(gè)模式的轉(zhuǎn)換就完成了
PHP編程鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。
新聞熱點(diǎn)
疑難解答
圖片精選