offload-project/laravel-toggle
Composer 安装命令:
composer require offload-project/laravel-toggle
包简介
A simple and flexible feature toggle (feature flag) package for Laravel
README 文档
README
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,@endtogglefor 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?
- Requirements
- Installation
- Quick Start
- Configuration
- Usage
- Artisan Commands
- AI Coding Assistant Skill
- Testing
- Contributing
- Security
- License
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_flagsalways use the database driver (with config fallback) - Flags in
flagsalways use the config driver (read-only) - Unlisted flags use the global
driversetting
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.
- Contributing Guide — setup, workflow, commit conventions, and PR process
- Code of Conduct — expectations for participation in this project
Security
- Security Policy — how to report a vulnerability privately
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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 offload-project/laravel-toggle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Pheature flags Doctrine DBAL toggle implementation library.
ZF2 module for the Opensoft Rollout library
Pheature flags toggle CRUD library.
A PHP trait to enable bitwise comparison of flags
Adds flags support to your model/entity
County and languages flags css
统计信息
- 总下载量: 723
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 22
- 点击次数: 25
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-04