How to Create Middleware with Parameters in Laravel?
Laravel is the best framework in PHP todays. Laravel framework provide saveral functionality and you can also find from this site. But now in this post you can learn how to create custom middleware with passing parameters in laravel 5 example and how to use middleware with route parameters in laravel 5. In this example you can learn how to add middlware with check user have access role for this route from scratch in your laravel application.
I also added how to create middleware in my previous post, you can see that also.
In this example i added middleware for check use have role access for this route. So i added < So first create RoleMiddleware middleware using bellow command:
Create Middleware
php artisan make:middleware RoleMiddleware
Ok, now you can found RoleMiddleware.php in app/Http/Middleware directory and open RoleMiddleware.php file and put bellow code on that file. In this file i check given parameter role is access for current login user or not.
app/Http/Middleware/RoleMiddleware.php
namespace App\Http\Middleware;
use Closure;
use Auth;
class RoleMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $role)
{
if (! $request->user()->hasRole($role)) {
return redirect()->route('home');
}
return $next($request);
}
}
Now we need to register and create aliase above middleware in Kernel.php file so first open Kernel.php and add bellow line.
app/Http/Kernel.php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
......
protected $routeMiddleware = [
......
'role' => \App\Http\Middleware\RoleMiddleware::class,
];
}
Now we are ready to use role middleware in routes.php file. so you can see how to use middleware in routes.php file.
app/Http/routes.php
Route::get('home', ['as'=>'home','uses'=>'HomeController@index']);
Route::group(['middleware' => 'role:admin'], function () {
Route::get('admins', ['as'=>'admins','uses'=>'HomeController@admins']);
});
OR
Route::get('home', ['as'=>'home','uses'=>'HomeController@index']);
Route::get('admins', ['as'=>'admins','uses'=>'HomeController@admins','middleware' => 'role:admin']);
you can pass also multiple parameters as you want....
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Blade Check if Array Key Exists Example
- How to Get Columns Names from Model in Laravel?
- FCM Push Notification in Laravel Example
- How to Create Custom Middleware in Laravel?
- How to Exclude Route from CSRF Middleware in Laravel?
- Laravel 5.7 Middleware Tutorial With Example
- How to Call Middleware from Controller in Laravel?
- How to create database seeder in Laravel 5.7?
- Laravel 5.7 Ajax Pagination Example
- How to Count Files in a Directory using Laravel?
- Mysql procedure with pagination in laravel?
- How to Set URL without Http of Other Site in Laravel?
- Laravel CKeditor 5 Image Upload Tutorial Example