国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

Laravel Repository 模式

2020-03-22 20:25:04
字體:
來源:轉載
供稿:網友
  • Repository 模式

    為了保持代碼的整潔性和可讀性,使用Repository Pattern是非常有用的。事實上,我們也不必僅僅為了使用這個特別的設計模式去使用Laravel,然而在下面的場景下,我們將使用OOP框架Laravel去展示如何使用repositories使我們的Controller層不再那么啰嗦、更加解耦和易讀。下面讓我們更深入的研究一下。

    不使用repositories

    其實使用Repositories并不是必要的,在你的應用中你完全可以不使用這個設計模式的前提下完成絕大多數的事情,然而隨著時間的推移你可能把自己陷入一個死角,比如不選擇使用Repositories會使你的應用測試很不容易,(swapping out implementations)具體的實現將會變的很復雜,下面我們看一個例子。
    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'));        }    }

      

    這是一個很典型的一段代碼使用Eloquent數據庫交互,這段代碼工作的很正常,但是controller層對于Eloquent而言將是緊耦合的。在此我們可以注入一個repository創建一個解耦類型的代碼版本,這個解耦的版本代碼可以使后續程序的具體實現更加簡單。

    使用repositories

    其實完成整個repository模式需要相當多的步驟,但是一旦你完成幾次就會自然而然變成了一種習慣了,下面我們將詳細介紹每一步。

    1.創建Repository文件夾

    首先我們需要在app文件夾創建自己Repository文件夾repositories,然后文件夾的每一個文件都要設置相應的命名空間。

    2: 創建相應的Interface

    第二步創建對應的接口,其決定著我們的repository類必須要實現的相關方法,如下例所示,在此再次強調的是命名空間一定要記得加上。
    HouseRepositoryInterface.php

    <?php namespace AppRepositories;interface HouseRepositoryInterface {    public function selectAll();        public function find($id);}

      

    3:創建對應的Repository

    現在我們可以創建我們repository類 來給我們干活了,在這個類文件中我們可以把我們的絕大多數的數據庫查詢都放進去,不論多么復雜。如下面的例子
    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:創建后端服務提供

    首先你需要理解所謂服務提供,請參考手冊服務提供者
    BackendServiceProvider.php

    <?php namespace AppRepositories;use IlluminateSupportSeriveProvider;class BackSerivePrivider extends ServiceProvider {    public function register()    {        $this->app->bind('AppRepositoriesHouseRepositoryInterface', 'AppRepositoriesDbHouseRepository');    }}

      

    當然你也可以新建一個文件夾主要放我們的provider相關文件。
    上面一段代碼主要說的是,當你在controller層使用類型提示HouseRepositoryInterface,我們知道你將會使用DbHouseRepository.

    5:更新你的Providers Array

    其實在上面的代碼中,我們已經實現了一個依賴注入,但如果我們要使用在此我們是需要手動去寫的,為了更為方面,我們需要增加這個providers到app/config/app.php 中的providers數組里面,只需要在最后加上AppRepositoriesBackendServiceProvider::class,

    6:最后使用依賴注入更新你的controller

    當我們完成上面的那些內容之后,我們在Controller只需要簡單的調用方法代替之前的復雜的數據庫調用,如下面內容:
    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'));    }}

      這樣 整個模式的轉換就完成了

    PHP編程

    鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

  • 發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 新丰县| 佛学| 安远县| 河南省| 濮阳县| 九龙坡区| 余干县| 蓬安县| 鄂尔多斯市| 广东省| 富裕县| 日喀则市| 张家口市| 潞西市| 金川县| 财经| 龙泉市| 庆阳市| 社会| 景东| 开阳县| 南靖县| 渝北区| 焦作市| 巴塘县| 德兴市| 武安市| 那曲县| 保山市| 乡宁县| 白玉县| 社会| 耿马| 龙口市| 太湖县| 湘阴县| 张北县| 娱乐| 林州市| 门源| 黄山市|