Skip to content

Instantly share code, notes, and snippets.

@The-Hasanov
Last active April 9, 2019 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save The-Hasanov/1b3e29c27646ad03137e2669e20fa5aa to your computer and use it in GitHub Desktop.
Save The-Hasanov/1b3e29c27646ad03137e2669e20fa5aa to your computer and use it in GitHub Desktop.
Laravel Except route middleware without controller
<?php
\Illuminate\Routing\Route::macro('except', function ($excepts) {
/**
* @var Illuminate\Routing\Route $this
*/
$excepts = is_array($excepts) ? $excepts : func_get_args();
foreach ($excepts as $middleware) {
if (($index = array_search($middleware, $this->action['middleware'], true)) !== false) {
unset($this->action['middleware'][$index]);
}
}
$this->action['middleware'] = array_values($this->action['middleware']);
});
Route::macro('except', function ($excepts, $call) {
$excepts = is_array($excepts) ? $excepts : [$excepts];
/**
* @var \Illuminate\Routing\Router $this
*/
$before_count = $this->getRoutes()->count();
$call();
$routes = $this->getRoutes();
if ($routes->count() > $before_count) {
foreach (array_slice($routes->getRoutes(), -1 * ($routes->count() - $before_count)) as $key => $route) {
/**
* @var \Illuminate\Routing\Route $route
*/
$route->except($excepts);
}
}
});
// Usage
Route::get('modal/{transaction}', [
'uses' => 'PaymentController@modal',
'as' => 'payment.modal',
'middleware' => ['payment']
])->except(['auth:api']);
//Or
Route::except(['auth:api'], function () {
Route::get('modal/{transaction}', [
'uses' => 'PaymentController@modal',
'as' => 'payment.modal',
'middleware' => 'payment'
]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment