ziffmedia/laravel-cloudflare 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ziffmedia/laravel-cloudflare

Composer 安装命令:

composer require ziffmedia/laravel-cloudflare

包简介

A Laravel Nova tool.

README 文档

README

Features

  • Laravel Nova Tool for adding a cache purge page to manually purge specific urls in batches
  • Laravel Nova Field for cache purging specific urls or Cache Tags for a resource
  • Option to include either the Tool, Field, or both in project
  • Option to use urls, Cache Tag header, or both for purging Cloudflare cache
Nova Tool:

Nova Tool Screenshot

Nova Field:

Nova Field Screenshot

Installation

  • Install the package into your Laravel application.
composer require ziffmedia/laravel-cloudflare
  • Publish the config file to your application.
   php artisan vendor:publish --provider="ZiffMedia\LaravelCloudflare\CloudflareServiceProvider" --tag="config" 
  • Provide the new config file your Cloudflare Zone and API keys in your .env files.

Nova Tool Integration

To provide the cache purge Tool in your Nova instance, you must register the Tool with your NovaServiceProvider:

// app/Providers/NovaServiceProvider.php

use ZiffMedia\LaravelCloudflare\Nova\Tools\LaravelCloudflareTool;

public function tools()
{
    return [
        // ...,
        LaravelCloudflareTool::make()
    ];
}

For adding permissions to the Tool, please see the Nova documentation.

Note: The Tool is intended only for urls; setup for Cache Tags is not required if only using the Tool or url purging.

Nova Field Integration

To provide the Cache Purge button on any resource, you must reference the Field and make sure that you have setup the appropriate purge method (url or Cache Tag).

// app/Nova/

use ZiffMedia\LaravelCloudflare\Nova\Fields\ClearCacheButton;

public function fields(Request $request)
{
    ClearCacheButton::make('Cloudflare Cache')
        ->purgeUrls(
            ['https://www.example.com', 'https://www.example.com/page']
        )
        ->purgeTags(function () {
            return $this->cloudflareTagsToClear(); // In order to use this method, you must setup Cache Tags (see below)
        })
        ->hideWhenCreating()
}

Cloudflare Cache-Tag Header Integration

Cloudflare has the ability to use a special Cache-Tag header on requests to purge multiple pages instantly. If you intend to only use url purging, you can skip this step. In order to setup the Cache-Tag header, follow the below steps:

  • Add the Cloudflare Cache-Tag middleware to automatically add the Cache-Tag header to all requests. Because Cloudflare automatically removes the headers, you can use debug_cache_tags=1 in the querystring of requests to see the output of the header.
// app/Http/Kernel.php

use ZiffMedia\LaravelCloudflare\Middleware\CloudflareTagHeaders;

protected $middlewareGroups = [
    'web' => [
        // ...
        CloudflareTagHeaders::class,
    ]
];
  • Add the Cloudflare controller concern to tell the page which tags to give the header request. Here is an example implementation for an Article resource:
// app/Http/Controllers/ArticleController.php

use ZiffMedia\LaravelCloudflare\Controllers\Concerns\CloudflareTaggable;

class ArticleController extends Controller
{
    use CloudflareTaggable;

    public function index()
    {
        // For a collection of models
        $articles = Article::get();
        $this->addCloudflareTagsFromCollection($articles); // This will add a tag to the page for each model in the collection
        
        // For a single model
        $article = Article::first();
        $this->addCloudflareTagFromModel($article); // This will add a tag to the page for a single model
                
        return view(...);
    }
}
  • Add the Cloudflare model concern to tell the headers which tag to use for a model. Additionally, the cloudflareTagsToClear and cloudflareTag methods can be overwritten to allow for customization of tags or purging of additional resources. Here is a basic example implementation for an Article model:
// app/Models/Article.php

use ZiffMedia\LaravelCloudflare\Models\Concerns\CloudflareTaggable;

class Article extends Model
{
    use CloudflareTaggable;
    // ...   
}

Config Options

  • purge_enabled: Allows for purging to be turned on or off
  • email: Email address used for authenticating with Cloudflare API
  • key: Key used to authenticate with Cloudflare API
  • zone: Zone used for Cloudflare API
  • domains: An array of domains which are allowed to be purged. All url purge requests will be validated against these domains, if any.

ziffmedia/laravel-cloudflare 适用场景与选型建议

ziffmedia/laravel-cloudflare 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 130.5k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2020 年 08 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「nova」 「laravel」 「cloudflare」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ziffmedia/laravel-cloudflare 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ziffmedia/laravel-cloudflare 我们能提供哪些服务?
定制开发 / 二次开发

基于 ziffmedia/laravel-cloudflare 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 130.5k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 21
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-08-12