承接 offload-project/laravel-toggle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

offload-project/laravel-toggle

Composer 安装命令:

composer require offload-project/laravel-toggle

包简介

A simple and flexible feature toggle (feature flag) package for Laravel

README 文档

README

Latest Version on Packagist GitHub Tests Action Status Total Downloads

Laravel Toggle

A simple and flexible feature toggle (feature flag) package for Laravel. Control feature rollouts with config-based flags, database-driven toggles, or both.

Features

  • Two storage drivers - Config (environment variables) or Database (runtime toggleable)
  • Per-flag driver routing - Mix config and database flags in the same app via database_flags
  • Layered approach - Database driver falls back to config, allowing gradual migration
  • Built-in caching - Configurable cache store and TTL for performance
  • Blade directives - @toggle, @elsetoggle, @endtoggle for clean templates
  • Enum support - Use backed enums for type-safe toggle names
  • Artisan commands - Scaffold new toggles and manage cache
  • Inertia integration - Share all flags with the frontend via middleware
  • Configurable defaults - Return false, true, or throw exceptions for undefined toggles

Table of Contents

Why Laravel Toggle?

Laravel's first-party Pennant package is designed for user-segmented rollouts and A/B testing. Laravel Toggle is simpler — it's for global on/off switches controlled by environment variables or database records, with no user resolution or driver complexity.

Requirements

  • PHP 8.3+
  • Laravel 11/12/13

Installation

composer require offload-project/laravel-toggle

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

If using the database driver, publish and run the migrations:

php artisan vendor:publish --tag=toggle-migrations
php artisan migrate

Quick Start

Define a toggle in config

Add your feature flags to config/toggle.php:

'flags' => [
    'new-checkout' => env('TOGGLE_NEW_CHECKOUT', false),
    'dark-mode' => env('TOGGLE_DARK_MODE', true),
],

Check toggles in code

use OffloadProject\Toggle\Facades\Toggle;

if (Toggle::active('new-checkout')) {
    // New checkout flow
}

if (Toggle::inactive('dark-mode')) {
    // Light mode only
}

Use Blade directives

@toggle('new-checkout')
    <x-new-checkout-form />
@elsetoggle
    <x-legacy-checkout-form />
@endtoggle

Configuration

Driver

Set the driver in your .env file:

TOGGLE_DRIVER=config   # Read-only, uses config/toggle.php flags
TOGGLE_DRIVER=database # Read-write, falls back to config

The config driver is read-only at runtime — values come from environment variables and config files.

The database driver checks the database first, then falls back to config values. This allows you to define defaults in config while overriding specific toggles at runtime.

Per-flag driver routing

You can mix both drivers in the same application. Define config-driven flags in flags and list database-driven flags in database_flags:

// config/toggle.php

'flags' => [
    // Config-driven, read-only — controlled by .env
    'new-checkout' => env('TOGGLE_NEW_CHECKOUT', false),
    'dark-mode' => env('TOGGLE_DARK_MODE', true),
],

'database_flags' => [
    // Database-driven, mutable at runtime via Toggle::enable() / Toggle::disable()
    'maintenance-banner',
    'beta-access',
],

Resolution logic:

  • Flags in database_flags always use the database driver (with config fallback)
  • Flags in flags always use the config driver (read-only)
  • Unlisted flags use the global driver setting

This lets you keep stable flags in .env while allowing runtime control over flags that need to change without a deploy.

Default behavior for undefined toggles

TOGGLE_DEFAULT=false     # Return false (default)
TOGGLE_DEFAULT=true      # Return true
TOGGLE_DEFAULT=exception # Throw ToggleNotFoundException

Caching

TOGGLE_CACHE_ENABLED=true
TOGGLE_CACHE_STORE=redis  # null uses default cache store
TOGGLE_CACHE_TTL=3600     # seconds

Usage

Facade methods

use OffloadProject\Toggle\Facades\Toggle;

// Check if active
Toggle::active('feature-name');    // bool
Toggle::inactive('feature-name');  // bool

// Modify toggles (database driver only)
Toggle::enable('feature-name');   // Enable a toggle
Toggle::disable('feature-name');  // Disable a toggle
Toggle::delete('feature-name');   // Remove from database

// Get all toggles
Toggle::all(); // ['feature-name' => true, ...]

// Cache management
Toggle::forgetCache('feature-name'); // Clear specific toggle
Toggle::flushCache();                // Clear all toggles

Using enums

Define your toggles as a backed enum for type safety:

enum Feature: string
{
    case NewCheckout = 'new-checkout';
    case DarkMode = 'dark-mode';
    case BetaFeatures = 'beta-features';
}

Use the enum directly:

use App\Enums\Feature;

if (Toggle::active(Feature::NewCheckout)) {
    // ...
}

Toggle::enable(Feature::BetaFeatures);

Eloquent model

When using the database driver, you can also use the Toggle model directly:

use OffloadProject\Toggle\Models\Toggle;

// Query toggles
$toggle = Toggle::where('name', 'new-checkout')->first();

// Create or update
Toggle::updateOrCreate(
    ['name' => 'new-checkout'],
    ['active' => true]
);

The model automatically clears the cache when toggles are saved or deleted.

Inertia

Share all toggles with your frontend by using the provided Inertia middleware. Replace your HandleInertiaRequests middleware in bootstrap/app.php:

use OffloadProject\Toggle\Middleware\ShareTogglesWithInertia;

->withMiddleware(function (Middleware $middleware) {
    $middleware->web(append: [
        ShareTogglesWithInertia::class,
    ]);
})

All toggles will be available as the flags prop in your frontend:

// Vue/React
const {flags} = usePage().props

if (flags.newCheckout) {
    // Show new checkout
}

Artisan Commands

List all toggles

php artisan toggle:list

Displays a table of all defined toggles and their current state.

Create a toggle

# Create a config-based toggle
php artisan toggle:create new-feature

# Create as active by default
php artisan toggle:create new-feature --active

# Also create a database record
php artisan toggle:create new-feature --active --db

This command will:

  • Add the flag to config/toggle.php
  • Add the environment variable to .env
  • Optionally create a database record

Clear cache

# Clear all toggle caches
php artisan toggle:cache-clear

# Clear a specific toggle
php artisan toggle:cache-clear new-feature

AI Coding Assistant Skill

This package ships a Laravel Boost skill so coding assistants (Claude Code, Cursor, etc.) follow the package's conventions when generating code. Install it in your app with:

php artisan boost:add-skill offload-project/laravel-toggle

The skill source lives at skills/SKILL.md.

Testing

composer test

Contributing

Contributions are welcome! Please see the documents below before getting started.

Security

License

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

offload-project/laravel-toggle 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-04