I would like to require login for all pages, except /index
and /login
. I tried to solve it like this:
Route::filter('pattern: ^(index|login)*', 'auth');
In laravel 8 Route::filter
is not available anymore. How to solve this issue, without putting every route definition into a large group?
CodePudding user response:
Create a middleware group in your routes/web.php file.
Route::middleware(['auth'])->group( function () {
// Your protected routes here
});
Alternatively you can make middleware exceptions for routes:
// In your auth middleware that extends Authenticate
...
/**
* Routes that should skip handle.
*
* @var array $except
*/
protected array $except;
public function __construct(){
$this->except = [
route('index'),
route('login')
];
}
/**
* @param \Illuminate\Http\Request $request
*/
public function handle( Request $request )
{
$current_route = $request->route();
if( in_array($current_route, $this->except ) {
// Route don't needs to be guarded
}
}
...
If you don't want to have large groups then you can specify the middleware per route (can look messy)
Route::get('/route', [FooController::class, 'index'])->middleware('auth');