Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling / Disabling Features in a Laravel App

I'm building a Laravel app, which has a number of various features. I want to be able to enable or disable them depending on a particular domain's requirement. Currently, I have in my config a series of flags such as:

'is_feature_1_enabled' => true,
'is_feature_2_enabled' => false,

... and so on.

Then in my controllers and views, I check those config values to see whether or not I should be displaying something, allowing certain actions, etc. My app is starting to get polluted with these kinds of checks everywhere.

Is there a best practice method of managing features in a Laravel app?

like image 973
StackOverflowNewbie Avatar asked Jan 05 '20 12:01

StackOverflowNewbie


People also ask

How do I enable developer mode in Laravel?

Open the app. php file located in your config/app. php laravel project. Search for debug key and change true to enable debug mode and false for disabling debug mode default it will show false.

How do I turn on maintenance mode in Laravel 8?

Show activity on this post. I am using this command to enable maintenance mode php artisan down --secret="1630542a-246b-4b66-afa1-dd72a4c43515 . And then, I access my site with exapmple.com/1630542a-246b-4b66-afa1-dd72a4c43515 , to bypass the maintenance mode.

What is maintenance mode in Laravel?

What is maintenance mode? Maintenance mode in Laravel prevents users from accessing the application. This is usually done when you want to carry out an update, upgrade, or debug errors from the application. You can set your application to maintenance mode by running the following command: php artisan down.

How do I add a controller in Laravel?

Open the command prompt or terminal based on the operating system you are using and type the following command to create controller using the Artisan CLI (Command Line Interface). Replace the <controller-name> with the name of your controller. This will create a plain constructor as we are passing the argument — plain.


2 Answers

This is technically called feature flags - https://martinfowler.com/articles/feature-toggles.html

depends on your requirements, flags in config/database, rollout, etc...

But it's basically if's in code and cannot be clean.

Laravel packages:

https://github.com/alfred-nutile-inc/laravel-feature-flag

https://github.com/francescomalatesta/laravel-feature

Some services:

https://launchdarkly.com/

https://bullet-train.io/

https://configcat.com/

Also look at https://marketingplatform.google.com/about/optimize/ for frontend.

like image 92
Daniel Šádek Avatar answered Oct 17 '22 03:10

Daniel Šádek


I've encountered the same problem when I tried to implement multiple hotel providers.

What I did was using service container.

first you will create class for each domain With his features:

  • like Doman1.php ,Domain2.php
  • then inside each one of those you will add your logic.

then you gonna use binding in your app service provider to bind the domain with class to use.

$this->app->bind('Domain1',function (){
       return new Domain1();
    });
    $this->app->bind('Domain2',function (){
        return new Domain2();
    });

Note you can use general class that holds the features goes with all domains then use that general class in your classes

Finally in your controller you can check your domain then to use the class you gonna use

    app(url('/'))->methodName();
like image 38
M.abdelrhman Avatar answered Oct 17 '22 03:10

M.abdelrhman