How to create and use Middleware in Laravel?

How to create and use Middleware in Laravel?

In this post we will give you information about How to create and use Middleware in Laravel?. Hear we will give you detail about How to create and use Middleware in Laravel?And how to use it also give you demo for it if it is necessary.

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 as filter in laravel 5 example and how to use middleware in laravel 5. In this example you can learn how to add middlware from scratch in your laravel application.

In this example i added middleware for check if user is admin then it can open someroute like ACL. So i added

id_admin column in my users table if use have is_admin = 1 then it can access “admins” route. So first create IsAdminMiddleware middleware using bellow command:

Create Middleware

php artisan make:middleware IsAdminMiddleware

Ok, now you can found IsAdminMiddleware.php in app/Http/Middleware directory and open IsAdminMiddleware.php file and put bellow code on that file. In this file i check first if user is not login then it will redirect home route and other if user have not is_admin = 1 then it will redirect home route too.

app/Http/Middleware/IsAdminMiddleware.php

namespace AppHttpMiddleware;

use Closure;

use Auth;

class IsAdminMiddleware

{

/**

* Handle an incoming request.

*

* @param IlluminateHttpRequest $request

* @param Closure $next

* @return mixed

*/

public function handle($request, Closure $next)

{

if(!Auth::check() || Auth::user()->is_admin != '1'){

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 AppHttp;

use IlluminateFoundationHttpKernel as HttpKernel;

class Kernel extends HttpKernel

{

......

protected $routeMiddleware = [

......

'is-admin' => AppHttpMiddlewareIsAdminMiddleware::class,

];

}

Now we are ready to use is-admin 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' => 'is-admin'], function () {

Route::get('admins', ['as'=>'admins','uses'=>'HomeController@admins']);

});

OR

Also see:Laravel 5.7 Middleware Tutorial With Example

Route::get('home', ['as'=>'home','uses'=>'HomeController@index']);


Route::get('admins', ['as'=>'admins','uses'=>'HomeController@admins','middleware' => 'is-admin']);

Hope this code and post will helped you for implement How to create and use Middleware in Laravel?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

9  +  1  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US