laravel middleware route
Route::middleware([CheckAge::class])->group(function () {
Route::get('/', function () {
//
});
Route::get('admin/profile', function () {
//
})->withoutMiddleware([CheckAge::class]);
});
laravel middleware route
Route::middleware([CheckAge::class])->group(function () {
Route::get('/', function () {
//
});
Route::get('admin/profile', function () {
//
})->withoutMiddleware([CheckAge::class]);
});
how to pass parameter to middleware laravel 8
Generally you can pass parameters to middleware via using : symbol like this:
Route::get('user/{id}', ['middleware' => 'auth:owner', function ($id) {
// Your logic here...
}]);
And get the passed parameter into middleware method like this:
<?php
namespace AppHttpMiddleware;
use Closure;
class Authentication
{
public function handle($request, Closure $next, $role)
{
if (auth()->check() && auth()->user()->hasRole($role)) {
return $next($request);
}
return redirect('login');
}
}
Note that the handle() method, which usually only takes a $request and a $next closure, has a third parameter, which is our middleware parameter.
If you passed in multiple parameters like auth:owner,subscription to your middleware call in the route definition, just add more parameters to your handle method which will look like this - handle($request, Closure $next, $role,$subscription)
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us