• This repository has been archived on 12/Mar/2024
  • Stars
    star
    168
  • Rank 218,415 (Top 5 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 2 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An opinionated feature flags package for Laravel.

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 wish to just create a new flag without updating existing ones, i.e. inside of a seeder, you can use Features::add().

use RyanChandler\LaravelFeatureFlags\Facades\Features;

Features::add('registration');

This will create a new flag that is disabled by default. To enable the flag by default, provide a boolean value to the enabled argument.

use RyanChandler\LaravelFeatureFlags\Facades\Features;

Features::add('registration', enabled: true);

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.

To get an array of all flags, use the Features::all() method. This will return an array where the flag names are used for the keys and a boolean representing the current state of the flag is the value.

use RyanChandler\LaravelFeatureFlags\Facades\Features;

$flags = Features::all();

foreach ($flags as $name => $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\WithFeatures;

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.

@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(),
]);
@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.

More Repositories

1

spruce

A lightweight state management layer for Alpine.js. 🌲
JavaScript
910
star
2

orbit

A flat-file database driver for Eloquent. 🗄
PHP
822
star
3

alpine-clipboard

Simply copy to the users clipboard in your Alpine.js components. 📋
JavaScript
317
star
4

alpine-tooltip

Add tooltips to your Alpine 3.x components with a custom directive.
JavaScript
289
star
5

laravel-comments

A dead-simple comments package for Laravel.
PHP
195
star
6

blade-cache-directive

Cache chunks of your Blade markup with ease. 🔪
PHP
186
star
7

laravel-cloudflare-turnstile

A simple package to help integrate Cloudflare Turnstile.
PHP
183
star
8

alpine-hooks

A collection of hooks for use with Alpine.js.
JavaScript
176
star
9

filament-navigation

Build structured navigation menus in Filament.
PHP
150
star
10

prettier-plugin-blade

Adds support for Blade templates to Prettier. (work in progress)
TypeScript
138
star
11

filament-profile

A simple profile management page for Filament. ✨
PHP
100
star
12

bearer

Minimalistic token-based authorization for Laravel API endpoints.
PHP
76
star
13

forge-previewer

Create preview deployments for pull requests with Laravel Forge.
PHP
69
star
14

blade-tabler-icons

A Blade icon pack for the Tabler icon set.
PHP
63
star
15

alpine-mask

A clean integration between Cleave.js and Alpine. ✨
JavaScript
60
star
16

cpx

Quickly execute Composer package binaries from anywhere. ⚡️
PHP
58
star
17

blade-capture-directive

Create inline partials in your Blade templates with ease.
PHP
54
star
18

using

Enforced disposal of objects in PHP. 🐘
PHP
53
star
19

is-php-dead.lol

Is PHP dead?
PHP
48
star
20

filament-progress-column

Add a progress bar column to your Filament tables.
PHP
47
star
21

filament-feature-flags

Control your Laravel feature flags through a clean Filament interface.
PHP
47
star
22

laravel-uuid

A small package for adding UUIDs to Eloquent models.
PHP
40
star
23

color

A simple Color object for PHP packages and applications. 🎨
PHP
38
star
24

cursed-html

Who said HTML wasn't a programming language?
JavaScript
37
star
25

filament-tools

Add a general-purpose tools page to your Filament project. 🛠
PHP
37
star
26

lexical

PHP
37
star
27

standalone-blade

Use Laravel's Blade templating engine outside of Laravel.
PHP
36
star
28

laravel-helpers

A collection of helper functions that I use across my projects.
PHP
34
star
29

phpast.com

A web tool to explore the ASTs generated by PHP-Parser.
PHP
32
star
30

laravel-json-settings

Store your Laravel application settings in an on-disk JSON file. ⚙️
PHP
31
star
31

lagoon

A dynamic, weakly-typed and minimal scripting language. 🏞
Rust
29
star
32

computed-properties

A small package to add computed properties to any PHP class. 🐘
PHP
27
star
33

phiki

A syntax highlighter for PHP inspired by Shiki.
PHP
27
star
34

fern

Persisted global stores for Alpine 3.x.
JavaScript
26
star
35

laravel-slug

Simple slugs for your Laravel models.
PHP
25
star
36

skeleton-laravel

PHP
25
star
37

x-else

An `x-else` directive for use in Alpine components. ✨
JavaScript
25
star
38

ryangjchandler.co.uk

My personal website, blog and project documentation.
PHP
22
star
39

laravel-expose

A clean integration between Laravel and Expose. ⚡️
PHP
21
star
40

filament-minimal-tabs

A clean and minimal design for the Tabs component in Filament.
CSS
20
star
41

alpine-parent

Adds a handy $parent magic property to your Alpine components.
JavaScript
18
star
42

alpine-toggle

Quickly toggle / negate a property in your Alpine.js components.
JavaScript
17
star
43

puny

Make unit testing in PHP simpler again. 👌
PHP
17
star
44

laravel-orphan-controller

Quickly identify controller methods with no route in your Laravel applications.
PHP
16
star
45

downmark

A speedy Markdown parser for PHP applications.
PHP
16
star
46

filament-log

A simplistic log viewer for your Filament apps.
PHP
15
star
47

stencil

A simple template engine for PHP written for a tutorial blog post.
PHP
15
star
48

git

A small PHP wrapper for Git. ✨
PHP
15
star
49

laravel-make-user

Quickly create `User` models with Artisan. ⚡️
PHP
14
star
50

fn-inspector

A utility package that helps inspect functions in PHP.
PHP
14
star
51

filament-user-resource

A simple resource for managing users in Filament.
PHP
13
star
52

tonic

An elegant language for script-kiddies and terminal squatters.
Rust
12
star
53

bytes

A class to help convert bytes into other units (kb, mb, etc).
PHP
12
star
54

laravel-make-view

A simple `make:view` command for Laravel applications.
PHP
10
star
55

laravel-rql

I don't like writing queries for CSV exports.
PHP
9
star
56

filament-color-palette

Let users pick a color from a fixed list of options.
PHP
8
star
57

fakeinator

Quickly generate CSV files full of fake data. 💽
HTML
7
star
58

container

A service container for PHP.
PHP
7
star
59

proxy

Proxy method and property interactions in PHP. ⚡️
PHP
7
star
60

laravel-auto-validate-models

A small package to auto-validate your Laravel models.
PHP
7
star
61

krypt

Share short-lived encrypted text messages with others.
PHP
6
star
62

uptime-checker

My personal uptime checker powered by Filament. ⚡️
PHP
6
star
63

repository

📦 A dead-simple repository class for use with Laravel.
PHP
5
star
64

rgjc.me

A disgustingly simple URL shortener powered by Laravel. 📶
PHP
5
star
65

laravel-restore-mix

A small CLI script that removes Vite in favour of Laravel Mix.
PHP
5
star
66

microphp

A small subset of PHP implemented in Rust. 🐘
Rust
5
star
67

rss-reader

An RSS reader application built with Laravel.
PHP
5
star
68

forge-previewer-action

A quick and easy way to get Forge Previewer running in GitHub Actions.
Dockerfile
5
star
69

paddle-demultiplexer

A simple serverless function to demultiplex Paddle webhooks.
TypeScript
4
star
70

phpbyexample.dev

A simple introduction to PHP with annotated code snippets.
CSS
4
star
71

template-rust-cli

A template for command-line Rust projects.
Python
4
star
72

peso

An elephant-ish esoteric programming language inspired by Brainf*ck.
PHP
4
star
73

notepal

My personal note-taking application.
PHP
3
star
74

typewrite.cc

A bring-your-own-email-first newsletter service. ✨
PHP
3
star
75

filament-plugin-search

Add a page to your Filament panel to search for plugins.
PHP
3
star
76

filament-simple-repeater

A single-field repeater for Filament. ⚡️
PHP
3
star
77

php-ffi-example

An example of using PHP + FFI with C, Rust and Go.
PHP
3
star
78

advent-of-code

Advent of Code Solutions in PHP
PHP
3
star
79

c-in-php

An incredibly cursed C interpreter written in PHP.
PHP
3
star
80

laravel-worldwide-demo

The demo used for my talk at October's Laravel Worldwide Meetup.
PHP
2
star
81

rook-lang

A small scripting language running on V8.
Rust
2
star
82

filament-data-studio

Low configuration exports and data analysis for Filament.
PHP
2
star
83

filament-passwordless-login

PHP
2
star
84

60-second-laravel

The demo application used in the 60 Second Laravel series.
PHP
2
star
85

pl

A toy language implemented in Rust. 🦀
Rust
2
star
86

curl-to-laravel-http

PHP
2
star
87

filament-plausible-page

Embed your site's Plausible dashboard inside of Filament.
PHP
2
star
88

laravel-slack-webhook

A fluent package for building Slack webhook payloads.
PHP
2
star
89

mathexpr

A tiny math expression evaluator written in PHP.
PHP
2
star
90

alpinejs-mobile

Swift
2
star
91

polling-app

A simple polling application built with the TALL stack. 🗼
PHP
2
star
92

laravel-restore-command

An Artisan command to quickly restore soft-deleted models. ⚡
PHP
2
star
93

php-parser-specs

A set of specification tests for PHP parsers, used to improve compliance and acceptance.
PHP
2
star
94

laravel-synchro

Easily sync and anonymise your data between environments.
PHP
2
star
95

blade-validator

Run rudimentary linting checks on your Blade templates.
PHP
1
star
96

expr

A powerful expression engine designed for business rules and sandboxed execution. 🔡
PHP
1
star
97

blade-in-directive

A tidy conditional Blade directive for checking if something is in an array.
PHP
1
star
98

chains

Gold, silver, diamonds, tiny ol' JavaScript framework.
JavaScript
1
star
99

symbol-interner

A simple string interner / symbol table for Rust projects.
Rust
1
star
100

filament-sanctum

Manage your Sanctum tokens inside of Filament. ✨
PHP
1
star