Skip to content
This repository was archived by the owner on Jun 23, 2025. It is now read-only.

Commit 5e54aaa

Browse files
authored
Add Middleware feature.enabled, feature.disabled (#26)
1 parent a31289f commit 5e54aaa

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

README.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,46 @@ Check back later for more features!
118118
@endfeatureDisabled
119119
```
120120

121-
You cannot currently use dynamic strategy arguments with Blade template directives.
121+
You cannot currently use dynamic strategy arguments with Blade template directives.
122+
123+
### Middleware
124+
125+
This package includes middleware that will deny routes depending on whether a feature is enabled or not.
126+
127+
To use the middle, add the following to your `app/Http/Kernel.php`:
128+
129+
```php
130+
protected $routeMiddleware = [
131+
// other middleware
132+
'feature.enabled' => \MikeFrancis\LaravelUnleash\Middleware\FeatureEnabled::class,
133+
'feature.disabled' => \MikeFrancis\LaravelUnleash\Middleware\FeatureDisabled::class,
134+
];
135+
```
136+
137+
You can then use the middleware in your routes:
138+
139+
```php
140+
Route::get('/new-feature-path', function () {
141+
//
142+
})->middleware('feature.enabled:myAwesomeFeature');
143+
144+
Route::get('/terrible-legacy-path', function () {
145+
//
146+
})->middleware('feature.disabled:myAwesomeFeature');
147+
```
148+
149+
or in your controllers like so:
150+
151+
```php
152+
class ExampleController extends Controller
153+
{
154+
public function __construct()
155+
{
156+
$this->middleware('feature.enabled:myAwesomeFeature');
157+
// or
158+
$this->middleware('feature.disabled:myAwesomeFeature');
159+
}
160+
}
161+
```
162+
163+
You cannot currently use dynamic strategy arguments with Middleware.

src/Middleware/FeatureDisabled.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace MikeFrancis\LaravelUnleash\Middleware;
4+
5+
use Closure;
6+
use Illuminate\Http\Request;
7+
use MikeFrancis\LaravelUnleash\Facades\Feature;
8+
9+
class FeatureDisabled
10+
{
11+
public function handle(Request $request, Closure $next, string $featureName)
12+
{
13+
if (Feature::enabled($featureName)) {
14+
abort(404);
15+
}
16+
17+
return $next($request);
18+
}
19+
}

src/Middleware/FeatureEnabled.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace MikeFrancis\LaravelUnleash\Middleware;
4+
5+
use Closure;
6+
use Illuminate\Http\Request;
7+
use MikeFrancis\LaravelUnleash\Facades\Feature;
8+
9+
class FeatureEnabled
10+
{
11+
public function handle(Request $request, Closure $next, string $featureName)
12+
{
13+
if (!Feature::enabled($featureName)) {
14+
abort(404);
15+
}
16+
17+
return $next($request);
18+
}
19+
}

0 commit comments

Comments
 (0)