Middleware can also receive additional custom parameters. For example, if your app needs to verify that the user has a given "role" before executing an action, you can create a RoleMiddleware middleware that accepts the role name as an extra parameter.
Extra parameters of the middleware will be passed in after the $next parameter:
<?php
namespace App\Http\Middleware;
use Closure;
class RoleMiddleware
{
/**
* Run the request filter.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string $role
* @return mixed
*/
public function handle($request, Closure $next, $role)
{
if (! $request->user()->hasRole($role)) {
// Redirect...
}
return $next($request);
}
}
Middleware parameters can be specified by separating the name and parameters of the middleware with : when defining routes. Multiple parameters should be separated by commas:
Route::put('post/{id}', ['middleware' => 'role:editor', function ($id) {
//
}]);
For details, please refer to: http://www.golaravel.com/laravel/docs/5.1/middleware/#middleware-parameters
Middleware parameters
Middleware can also receive additional custom parameters. For example, if your app needs to verify that the user has a given "role" before executing an action, you can create a
RoleMiddleware
middleware that accepts the role name as an extra parameter.Extra parameters of the middleware will be passed in after the
$next
parameter:Middleware parameters can be specified by separating the name and parameters of the middleware with
:
when defining routes. Multiple parameters should be separated by commas:For details, please refer to: http://www.golaravel.com/laravel/docs/5.1/middleware/#middleware-parameters