定制 secretwebmaster/laravel-cloudflare 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

secretwebmaster/laravel-cloudflare

Composer 安装命令:

composer require secretwebmaster/laravel-cloudflare

包简介

Modern PHP 8+ package for seamless Cloudflare API integration in Laravel applications. Manage zones, DNS records, firewall rules, and more.

README 文档

README

A modern PHP 8+ package for seamless Cloudflare API integration in Laravel applications. Manage zones, DNS records, firewall rules, analytics, and more with a clean, type-safe interface.

PHP Version Laravel License

Features

  • Modern PHP 8+ - Strict type hints, typed properties, and modern syntax
  • Comprehensive Coverage - Zones, DNS, Firewall, Settings, and more
  • Well Documented - Extensive PHPDoc blocks with parameter tables
  • Type Safe - Full type safety with strict typing throughout
  • Laravel Integration - Built specifically for Laravel using HTTP facade
  • Easy to Use - Intuitive API with consistent method naming
  • Free Tier Friendly - All free tier functions fully supported
  • Auto-Discovery - Automatic service provider registration

Requirements

  • PHP 8.0 or higher
  • Laravel 8.0 or higher
  • Cloudflare API Token or API Key

Installation

Install the package via Composer:

composer require secretwebmaster/laravel-cloudflare

Configuration

Publish the configuration file:

php artisan vendor:publish --tag=cloudflare-config

Add your Cloudflare credentials to your .env file:

CLOUDFLARE_API_TOKEN=your-api-token

Basic Usage

Initialize the Client

use Secretwebmaster\LaravelCloudflare\Client;

// Option 1: Create using .env values
$client = new Client();

// Option 2: Create manually with API Token
$client = new Client('your-api-token');

// Option 3: Create manually with Email + API Key
$client = new Client(null, 'your-email@example.com', 'your-api-key');

Working with Zones

The Zones model provides complete zone management functionality.

List All Zones

// List all zones
$response = $client->zones()->list();

// List zones with pagination
$zones = $client->zones()->list([
    'page' => 1,
    'per_page' => 20
]);

// Filter zones by status
$zones = $client->zones()->list([
    'status' => 'active'
]);

Get Zone by Domain

// Get zone information by domain name
$zone = $client->zones()->getByDomain('example.com');

Get Zone ID by Domain

// Quickly get just the zone ID
$zoneId = $client->zones()->getZoneIdByDomain('example.com');

Create a New Zone

$response = $client->zones()->create([
    'name' => 'example.com',
]);

Get Zone Details

$response = $client->zones()->get($zoneId);

Update Zone Settings

$client->zones()->edit($zoneId, [
    'paused' => false,
    'plan' => [
        'id' => 'plan-id'
    ]
]);

Purge Cache

// Purge everything
$client->zones()->purgeCache($zoneId, ['purge_everything' => true]);

// Purge specific files
$client->zones()->purgeCache($zoneId, [
    'files' => [
        'https://example.com/style.css',
        'https://example.com/script.js'
    ]
]);

// Purge by tags
$client->zones()->purgeCache($zoneId, [
    'tags' => ['static', 'images']
]);

// Purge by hostname
$client->zones()->purgeCache($zoneId, [
    'hosts' => ['www.example.com', 'api.example.com']
]);

Check Zone Activation

$response = $client->zones()->activationCheck($zoneId);

Delete Zone

$response = $client->zones()->delete($zoneId);

Working with DNS Records

The DnsRecords model handles all DNS record operations.

List DNS Records

// List all DNS records for a zone
$response = $client->dnsRecords()->list($zoneId);

// Filter by record type
$records = $client->dnsRecords()->list($zoneId, [
    'type' => 'A'
]);

// Filter by name
$records = $client->dnsRecords()->list($zoneId, [
    'name' => 'www.example.com'
]);

// Combine filters with pagination
$records = $client->dnsRecords()->list($zoneId, [
    'type' => 'A',
    'page' => 1,
    'per_page' => 50
]);

Create DNS Record

// Create an A record
$record = $client->dnsRecords()->create($zoneId, [
    'type' => 'A',
    'name' => 'www.example.com',
    'content' => '192.168.1.1',
    'ttl' => 1,  // 1 = Auto
    'proxied' => true
]);

// Create a CNAME record
$record = $client->dnsRecords()->create($zoneId, [
    'type' => 'CNAME',
    'name' => 'blog',
    'content' => 'example.com',
    'ttl' => 1,
    'proxied' => false
]);

// Create an MX record
$record = $client->dnsRecords()->create($zoneId, [
    'type' => 'MX',
    'name' => 'example.com',
    'content' => 'mail.example.com',
    'priority' => 10,
    'ttl' => 1
]);

// Create a TXT record
$record = $client->dnsRecords()->create($zoneId, [
    'type' => 'TXT',
    'name' => '_dmarc',
    'content' => 'v=DMARC1; p=reject; rua=mailto:admin@example.com',
    'ttl' => 1
]);

Get DNS Record Details

$response = $client->dnsRecords()->get($zoneId, $recordId);

Update DNS Record (Full Update)

// Update entire record
$client->dnsRecords()->update($zoneId, $recordId, [
    'type' => 'A',
    'name' => 'www.example.com',
    'content' => '192.168.1.2',
    'ttl' => 1,
    'proxied' => true
]);

Patch DNS Record (Partial Update)

// Update only specific fields
$client->dnsRecords()->edit($zoneId, $recordId, [
    'content' => '192.168.1.3',
    'proxied' => false
]);

// Just change TTL
$client->dnsRecords()->edit($zoneId, $recordId, [
    'ttl' => 3600
]);

Delete DNS Record

$response = $client->dnsRecords()->delete($zoneId, $recordId);

Advanced Usage

Zone Settings

Manage all zone settings with dedicated getter and setter methods.

// Get SSL setting
$response = $client->zoneSettings()->getSsl($zoneId);

// Update SSL setting
$client->zoneSettings()->updateSsl($zoneId, ['value' => 'full']);

// Get Always Use HTTPS
$alwaysHttps = $client->zoneSettings()->getAlwaysUseHttps($zoneId);

// Enable Always Use HTTPS
$client->zoneSettings()->updateAlwaysUseHttps($zoneId, ['value' => 'on']);

// Get and update multiple settings
$settings = $client->zoneSettings()->list($zoneId);

// Update any setting using the generic update method
$client->zoneSettings()->update($zoneId, 'minify', [
    'value' => [
        'css' => 'on',
        'html' => 'on',
        'js' => 'on'
    ]
]);

Firewall Access Rules

Manage IP access rules at zone, account, or user level.

// Zone-level: Block an IP
$client->firewallAccessRules()->create($zoneId, [
    'mode' => 'block',
    'configuration' => [
        'target' => 'ip',
        'value' => '192.168.1.100'
    ],
    'notes' => 'Blocked spam source'
]);

// Zone-level: Whitelist an IP range
$client->firewallAccessRules()->create($zoneId, [
    'mode' => 'whitelist',
    'configuration' => [
        'target' => 'ip_range',
        'value' => '10.0.0.0/8'
    ],
    'notes' => 'Office IP range'
]);

// Account-level: Challenge by country
$client->firewallAccessRules()->createAccount($accountId, [
    'mode' => 'challenge',
    'configuration' => [
        'target' => 'country',
        'value' => 'CN'
    ]
]);

// List zone access rules
$rules = $client->firewallAccessRules()->list($zoneId);

// Delete a rule
$client->firewallAccessRules()->delete($zoneId, $ruleId);

Page Rules

Create and manage page rules for URL-specific behaviors.

// Create a page rule
$client->pageRules()->create($zoneId, [
    'targets' => [
        [
            'target' => 'url',
            'constraint' => [
                'operator' => 'matches',
                'value' => 'example.com/admin/*'
            ]
        ]
    ],
    'actions' => [
        [
            'id' => 'ssl',
            'value' => 'strict'
        ],
        [
            'id' => 'always_use_https',
            'value' => true
        ]
    ],
    'priority' => 1,
    'status' => 'active'
]);

// List all page rules
$rules = $client->pageRules()->list($zoneId);

// Get available settings
$settings = $client->pageRules()->getSettingList();

Accounts & Users

// List accounts
$accounts = $client->accounts()->list();

// Get account details
$account = $client->accounts()->get($accountId);

// Get user details
$user = $client->users()->get();

// Update user
$client->users()->update([
    'first_name' => 'John',
    'last_name' => 'Doe'
]);

Available Models

All models are accessible through the client:

Model Description Methods
Zones Zone management list, create, get, edit, delete, purgeCache, etc.
DnsRecords DNS record management list, create, get, update, patch, delete, import, export
ZoneSettings Zone settings (86 methods) getSsl, updateSsl, getMinify, updateMinify, etc.
FirewallAccessRules IP access rules User/Account/Zone level CRUD operations
FirewallLockdowns Zone lockdown rules list, create, get, update, delete
FirewallUARules User Agent blocking list, create, get, update, delete
Waf Web Application Firewall Overrides, Packages, Groups, Rules management
PageRules Page rules list, create, get, update, delete
Accounts Account management list, get, details, update
Users User management get, details, update

Error Handling

All methods return the Cloudflare API response. Check the success field:

$response = $client->zones()->create(['name' => 'example.com']);

// Get error code explanations
$errorCodes = $client->zones()->errorCodes();
echo $errorCodes['1061']; // "An A, AAAA or CNAME record already exists..."

Testing

composer test

Changelog

Please see CHANGELOG for recent changes.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Security

If you discover any security-related issues, please email the package maintainer instead of using the issue tracker.

Credits

  • Author: SecretWebmaster
  • Built with ❤️ for the Laravel community

License

The MIT License (MIT). Please see License File for more information.

Resources

Support

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

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 34
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-18