An opinionated feature flags package for Laravel

Overview

An opinionated feature flags package for Laravel.

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package provides an opinionated API for implementing feature flags in your Laravel applications. It supports application-wide features as well as model specific feature flags.

Installation

You can install the package via Composer:

composer require ryangjchandler/laravel-feature-flags

You should then publish and run the migrations with:

php artisan vendor:publish --tag="feature-flags-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="feature-flags-config"

Usage

Global flags

To enable or disable a global feature flag, you can use the Features::enable() and Features::disable() methods respectively.

use RyanChandler\LaravelFeatureFlags\Facades\Features;

Features::enable(name: 'registration');
Features::disable(name: 'registration');

To check if a flag is enabled or disabled, use the Features::enabled() and Features::disabled() methods respectively.

use RyanChandler\LaravelFeatureFlags\Facades\Features;

if (Features::enabled(name: 'registration')) {
    // `registration` is enabled.
}

if (Features::disabled(name: 'registration')) {
    // `registration` is disabled.
}

If you simply want to toggle a flag, you can use the Features::toggle() method.

use RyanChandler\LaravelFeatureFlags\Facades\Features;

Features::toggle(name: 'registration');

If the flag is enabled, it will be disabled. If it's disabled, it will be enabled.

Model flags

If you would like to feature flag specific models, begin by implementing the RyanChandler\LaravelFeatureFlags\Models\Contracts\HasFeatures interface and using the RyanChandler\LaravelFeatureFlags\Models\Concerns\WithFeatures trait. Here's an example on a User model.

use RyanChandler\LaravelFeatureFlags\Models\Contracts\HasFeatures;
use RyanChandler\LaravelFeatureFlags\Models\Concerns\Features;

class User extends Authenticatable implements HasFeatures
{
    use WithFeatures;
}

The trait provides a default implementation that adheres to the interface. It's recommended that you always use this implementation instead of writing your own.

To enable, disable or toggle a flag, use the same Features::enable(), Features::disable() and Features::toggle() methods by providing a named argument for.

use RyanChandler\LaravelFeatureFlags\Facades\Features;

$user = User::first();

Features::enable('registration', for: $user);
Features::disable('registration', for: $user);
Features::toggle('registration', for: $user);

The WithFeatures trait also provides a few helper methods on the model: enableFeature(), disableFeature() and toggleFeature().

Blade directive

This package also provides a set of conditional Blade directives for protecting your views with feature flags.

Register now! @endfeature">
@feature('registration')
    <a href="/register">Register now!a>
@endfeature

You can use @elsefeature and @unlessfeature directives too.

If you would like to check a feature flag for a model, you can provide a named argument to the directive.

return view('my-view', [
    'user' => User::first(),
]);
Register now! @endfeature">
@feature('registration', for: $user)
    <a href="/register">Register now!a>
@endfeature

Middleware

This package provides a piece of middleware to protect your routes with feature flags.

You need to add the following code to your app/Http/Kernel.php file.

protected $routeMiddleware = [
    'feature' => \RyanChandler\LaravelFeatureFlags\Middleware\HasFeature::class,
];

You can then register middleware on your route like so:

Route::get('/register', fn () => ...)->middleware('feature:registration');

The default behaviour of the middleware is to abort with a 403 Forbidden status code.

This can be configured in the configuration file by changing the value of middleware.behaviour. The package uses the MiddlewareBehaviour enumeration as the configuration value.

You can change the status code using the middleware.code configuration option.

Redirecting instead of aborting

If you would prefer to redirect instead of aborting, set middleware.behaviour to MiddlewareBehaviour::Redirect and middleware.redirect to your preferred redirect location.

Multiple features

If you wish, you may protect your routes behind multiple feature flags. You can do this by comma-separating the flags passed when defining the middleware on your route definition:

Route::get('/feature', fn () => ...)->middleware('feature:verified,two-factor');

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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

Comments
Releases(v1.1.0)
  • v1.1.0(Oct 12, 2022)

    What's Changed

    • chore(deps): bump dependabot/fetch-metadata from 1.3.0 to 1.3.1 by @dependabot in https://github.com/ryangjchandler/laravel-feature-flags/pull/4
    • Fix import typo in example in readme by @tontonsb in https://github.com/ryangjchandler/laravel-feature-flags/pull/5
    • chore(deps): bump dependabot/fetch-metadata from 1.3.1 to 1.3.3 by @dependabot in https://github.com/ryangjchandler/laravel-feature-flags/pull/6
    • chore(deps): bump dependabot/fetch-metadata from 1.3.3 to 1.3.4 by @dependabot in https://github.com/ryangjchandler/laravel-feature-flags/pull/7
    • feature: add new Features::add() method by @ryangjchandler in https://github.com/ryangjchandler/laravel-feature-flags/pull/9
    • feature: add new Features::all() method by @ryangjchandler in https://github.com/ryangjchandler/laravel-feature-flags/pull/10

    New Contributors

    • @dependabot made their first contribution in https://github.com/ryangjchandler/laravel-feature-flags/pull/4
    • @tontonsb made their first contribution in https://github.com/ryangjchandler/laravel-feature-flags/pull/5
    • @ryangjchandler made their first contribution in https://github.com/ryangjchandler/laravel-feature-flags/pull/9

    Full Changelog: https://github.com/ryangjchandler/laravel-feature-flags/compare/v1.0.1...v1.1.0

    Source code(tar.gz)
    Source code(zip)
  • v1.0.1(Apr 7, 2022)

    What's Changed

    • Add support for multiple features in the middleware by @joelbutcher in https://github.com/ryangjchandler/laravel-feature-flags/pull/2

    New Contributors

    • @joelbutcher made their first contribution in https://github.com/ryangjchandler/laravel-feature-flags/pull/2

    Full Changelog: https://github.com/ryangjchandler/laravel-feature-flags/commits/v1.0.1

    Source code(tar.gz)
    Source code(zip)
  • v1.0.0(Apr 6, 2022)

Owner
Ryan Chandler
Ryan Chandler
A simple to use opinionated ERP package to work with Laravel

Laravel ERP A simple to use opinionated ERP package to work with Laravel Installation You can install the package via composer: composer require justs

Steve McDougall 16 Nov 30, 2022
An opinionated support package for Laravel, that provides flexible and reusable helper methods and traits for commonly used functionality.

Support An opinionated support package for Laravel, that provides flexible and reusable helper methods and traits for commonly used functionality. Ins

Ian Olson 3 Apr 14, 2021
An opinionated package to create slugs for Eloquent models

Generate slugs when saving Eloquent models This package provides a trait that will generate a unique slug when saving any Eloquent model. $model = new

Spatie 1.1k Jan 4, 2023
Laravel Segment is an opinionated, approach to integrating Segment into your Laravel application.

Laravel Segment Laravel Segment is an opinionated, approach to integrating Segment into your Laravel application. Installation You can install the pac

Octohook 13 May 16, 2022
An opinionated blade template formatter for Laravel that respects readability

blade-formatter An opinionated blade template formatter for Laravel that respects readability Online Demo Features Automatically Indents markup inside

Shuhei Hayashibara 277 Dec 26, 2022
this package can help you to test race condition in Laravel Feature Test

Laravel Async Testing this package can help you to test race condition in Laravel Feature Test Requirements Laravel versions 5.7, 6.x, 7.x and 8.x PHP

Recca Tsai 61 Nov 5, 2022
A package that adds view-composer like feature to Inertia.js Laravel adapter

Kinetic A package that adds view-composer like feature to Inertia.js Laravel adapter. Use to be able to share props based on the Inertia component nam

Marvin Quezon 76 Dec 12, 2022
Feature Switching made easy in Laravel 5

Feature Switching (made easy) for Laravel Need to wrap new features for dev and production? Use a directive in the view or alias in the controller The

Jonathan Bird 24 Dec 1, 2022
Cascade delete and restore when using the Laravel or Lumen SoftDeletes feature.

Cascade delete and restore when using the Laravel or Lumen SoftDeletes feature.

Asked.io 669 Nov 30, 2022
Jetstrap is a lightweight laravel 8 package that focuses on the VIEW side of Jetstream / Breeze package installed in your Laravel application

A Laravel 8 package to easily switch TailwindCSS resources generated by Laravel Jetstream and Breeze to Bootstrap 4.

null 686 Dec 28, 2022
A Laravel chat package. You can use this package to create a chat/messaging Laravel application.

Chat Create a Chat application for your multiple Models Table of Contents Click to expand Introduction Installation Usage Adding the ability to partic

Tinashe Musonza 931 Dec 24, 2022
This package provides extended support for our spatie/enum package in Laravel.

Laravel support for spatie/enum This package provides extended support for our spatie/enum package in Laravel. Installation You can install the packag

Spatie 264 Dec 23, 2022
Testbench Component is the de-facto package that has been designed to help you write tests for your Laravel package

Laravel Testing Helper for Packages Development Testbench Component is the de-facto package that has been designed to help you write tests for your La

Orchestra Platform 1.9k Dec 29, 2022
🥳🔐 This package is a Laravel package that checks if an email address is a spammer

This package is a Laravel package that checks if an email address is a spammer. It verifies your signups and form submissions to confirm that they are legitimate.

Endurance, the Martian 15 Dec 19, 2022
GeoLocation-Package - This package helps you to know the current language of the user, the country from which he is browsing, the currency of his country, and also whether he is using it vpn

GeoLocation in PHP (API) ?? ?? ?? This package helps you to know a lot of information about the current user by his ip address ?? ?? ?? This package h

Abdullah Karam 4 Dec 8, 2022
Laravel User Activity Log - a package for Laravel 8.x that provides easy to use features to log the activities of the users of your Laravel app

Laravel User Activity Log - a package for Laravel 8.x that provides easy to use features to log the activities of the users of your Laravel app

null 9 Dec 14, 2022
List of 77 languages for Laravel Framework 4, 5, 6, 7 and 8, Laravel Jetstream , Laravel Fortify, Laravel Breeze, Laravel Cashier, Laravel Nova and Laravel Spark.

Laravel Lang In this repository, you can find the lang files for the Laravel Framework 4/5/6/7/8, Laravel Jetstream , Laravel Fortify, Laravel Cashier

Laravel Lang 6.9k Jan 2, 2023
A package that uses blade templates to control how markdown is converted to HTML inside Laravel, as well as providing support for markdown files to Laravel views.

Install Install via composer. $ composer require olliecodes/laravel-etched-blade Once installed you'll want to publish the config. $ php artisan vendo

Ollie Codes 19 Jul 5, 2021
A light weight laravel package that facilitates dealing with arabic concepts using a set of classes and methods to make laravel speaks arabic

A light weight laravel package that facilitates dealing with arabic concepts using a set of classes and methods to make laravel speaks arabic! concepts like , Hijri Dates & Arabic strings and so on ..

Adnane Kadri 49 Jun 22, 2022