首頁  >  文章  >  php框架  >  聊聊Laravel怎麼使用 PHP 的裝飾器模式

聊聊Laravel怎麼使用 PHP 的裝飾器模式

青灯夜游
青灯夜游轉載
2022-09-20 11:12:352263瀏覽

如何在 Laravel 中使用 PHP 的裝飾模式?以下這篇文章就來跟大家介紹Laravel使用PHP裝飾器模式的方法,希望對大家有幫助!

聊聊Laravel怎麼使用 PHP 的裝飾器模式

設計模式對每個開發人員都很重要。它解決了您構建的每個項目中非常常見的問題。

裝飾器模式定義:

它可以幫助您在一個物件上添加額外的行為,而又不會影響同一類別中的其他對象。

維基百科:

#裝飾器模式是一種設計模式,它允許動態地將行為添加到單一對象,而不會影響同一類別中其他物件的行為

問題

假設我們有一個Post模型

class Post extends Model
{
    public function scopePublished($query) {
        return $query->where('published_at', '<=&#39;, &#39;NOW()&#39;);
    }
}

在我們的PostsController中,我們有如下的index方法

class PostsController extends Controller
{
    public function index() {
        $posts = Post::published()->get();
        return $posts;
    }
}

為了快取帖子並避免每次我們需要列出帖子時都查詢資料庫,我們可以執行以下操作

class PostsController extends Controller
{
    public function index() {
        $minutes = 1440; # 1 day
        $posts = Cache::remember('posts', $minutes, function () {
            return Post::published()->get();
        });
        return $posts;
    }
}

現在,我們將帖子快取1天。但看看程式碼,控制器了解太多了。它知道我們緩存了多少天,它自己緩存了物件。

同樣,假設您正在為HomePageController的Tag,Category,Archives實現相同的功能。閱讀和維護的程式碼太多了。

倉庫模式

在大多數情況下,倉庫模式是連接到裝飾器模式。

首先,讓我們使用倉庫模式分離取得貼文的方式,建立具有以下內容的app/Repositories/Posts/PostsRepositoryInterface.php

#
namespace App\Repositories\Posts;

interface PostsRepositoryInterface 
{

    public function get();

    public function find(int $id);

}

在同目錄下創建具有下面內容的PostsRepository

namespace App\Repositories\Posts;

use App\Post;

class PostsRepository implements PostsRepositoryInterface
{
    protected $model;

    public function __construct(Post $model) {
        $this->model = $model;
    }

    public function get() {
        return $this->model->published()->get();
    }

    public function find(int $id) {
        return $this->model->published()->find($id);
    }

}

回到PostsController並將更改應用為

namespace App\Http\Controllers;

use App\Repositories\Posts\PostsRepositoryInterface;
use Illuminate\Http\Request;

class PostsController extends Controller
{
    public function index(PostsRepositoryInterface $postsRepo) {
        return $postsRepo->get();
    }
}

控制器變得健康,知道足夠的細節來完成工作。

在這裡,我們依靠 Laravel 的 IOC 注入 Posts 介面的具體物件來取得我們的貼文

我們需要做的就是告訴Laravel的IOC使用介面時要建立哪個類別。

在你的app/Providers/AppServiceProvider.php 添加綁定方法

namespace App\Providers;

use App\Repositories\Posts\PostsRepositoryInterface;
use App\Repositories\Posts\PostsRepository;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind(PostsRepositoryInterface::class,PostsRepository::class);
    }
}

現在無論何時我們注入PostsRepositoryInterface Laravel 都會創建PostsRepository 的實例並將其傳回。

透過裝飾器實作快取

我們在一開始就說過,裝飾器模式允許將行為添加到單一對象,而不會影響同一類別中的其他物件。

在這裡快取是行為,物件/類別是 PostsRepository

讓我們在app/Repositories/Posts/PostsCacheRepository.php 中建立具有以下內容的PostsCacheRepository

#
namespace App\Repositories\Posts;

use App\Post;
use Illuminate\Cache\CacheManager;

class PostsCacheRepository implements PostsRepositoryInterface
{
    protected $repo;

    protected $cache;

    const TTL = 1440; # 1 day

    public function __construct(CacheManager $cache, PostsRepository $repo) {
        $this->repo = $repo;
        $this->cache = $cache;
    }

    public function get() {
        return $this->cache->remember('posts', self::TTL, function () {
            return $this->repo->get();
        });
    }

    public function find(int $id) {
        return $this->cache->remember('posts.'.$id, self::TTL, function () {
            return $this->repo->find($id);
        });
    }
}

在這個類別中,我們接受Caching 物件和PostsRepository 對象,然後使用類別(裝飾器)將快取行為加入到PostsReposiory 實例。

我們可以使用相同的範例將HTTP請求傳送到某些服務,然後在失敗的情況下傳回模型。我相信您會從該模式以及它如何輕鬆添加行為中受益。

最後一件事是修改 AppServiceProvider 介面綁定以建立 PostsCacheRepository 實例而不是PostsRepository

namespace App\Providers;

use App\Repositories\Posts\PostsRepositoryInterface;
use App\Repositories\Posts\PostsCacheRepository;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind(PostsRepositoryInterface::class,PostsCacheRepository::class);
    }
}

現在再次檢查文件,您會發現它非常易於閱讀和維護。同樣,它也是可測試的,如果您決定在某個時候刪除快取層。您只需在AppServiceProvider中變更綁定。無需額外更改。

結論

  • 我們學習如何使用修飾器模式快取模型
  • 我們展示了倉庫模式如何連接到修飾器模式
  • 依附註入和Laravel IOC如何使我們的生活變得輕鬆
  • laravel組件功能強大
##希望您喜歡閱讀本文。它向您展示了強大的設計模式,以及如何使您的專案易於維護和管理

原文網址:https://dev.to/ahmedash95/design-patterns-in-php- decorator-with-laravel-5hk6

【相關建議:

laravel影片教學

以上是聊聊Laravel怎麼使用 PHP 的裝飾器模式的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:learnku.com。如有侵權,請聯絡admin@php.cn刪除