Enabling / Disabling Features in a Laravel App Enabling / Disabling Features in a Laravel App laravel laravel

Enabling / Disabling Features in a Laravel App


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();


Look like you are hard coding things based on config values to enable or disable certain features. I recommend you to control things based on named routes rather than config value.

  1. Group all the route as a whole or by feature wise.
  2. Define name for all routes
  3. Control the enable/disable activity by route name and record in database
  4. Use Laravel middleware to check whether a particular feature is enabled or disabled by getting the current route name from request object and matching it with the database..

so you will not have the same conditions repeating every where and bloat your code..here is a sample code show you how to retrieve all routes, and you can match the route group name to further process to match your situation.

Route::get('routes', function() {$routeCollection = Route::getRoutes();echo "<table >";    echo "<tr>";        echo "<td width='10%'><h4>HTTP Method</h4></td>";        echo "<td width='10%'><h4>Route</h4></td>";        echo "<td width='80%'><h4>Corresponding Action</h4></td>";    echo "</tr>";    foreach ($routeCollection as $value) {        echo "<tr>";            echo "<td>" . $value->getMethods()[0] . "</td>";            echo "<td>" . $value->getPath() . "</td>";            echo "<td>" . $value->getName() . "</td>";        echo "</tr>";    }echo "</table>";});

and here is a sample middleware handler where you can check whether a particular feature is active by matching with what you have already stored in your database..

public function handle($request, Closure $next)    {        if(Helper::isDisabled($request->route()->getName())){             abort(403,'This feature is disabled.');        }        return $next($request);    }