Skip to main content

Explain Laravel’s Middleware?

Explain Laravel’s Middleware?

As the name suggests, Middleware acts as a middleman between request and response. It is a type of filtering mechanism. For example, Laravel includes a middleware that verifies whether the user of the application is authenticated or not. If the user is authenticated, he will be redirected to the home page otherwise, he will be redirected to the login page.
There are two types of Middleware in Laravel.
Global Middleware: will run on every HTTP request of the application.
Route Middleware: will be assigned to a specific route.
Hope you are doing great. You are here because you want to explore new possibilities to create robust and scalable PHP application using Laravel Framework. In this tutorial, we are going to cover topic’s like

What are middlewares in Laravel?
Types of Middleware in Laravel?
How to use middleware in Laravel 5 ?
How to create custom middleware in Laravel 5.5?

What are middlewares in Laravel

In Laravel, you can think middleware as a system or tool that are used to filter all HTTP requests entering your application. Middleware works between request and response of our application. It sits between Request and Response like a firewall that examine whether to route a request. Laravel comes with several inbuilt middlewares like EncryptCookies , RedirectIfAuthenticated, trim strings, TrustProxies etc. All middlewares are located in the app/Http/Middleware directory of  Laravel App.

Types of middlewares in Laravel?

There are three types of middleware are available in Laravel.

Global Middleware
Route Middleware groups
Route Middleware

How to use middleware in Laravel 5

Global Middleware: They run on every HTTP request of the application.Can be assigned in “$middleware” array of Kernel class which is located at app/Http/Kernel.php.
Route Middleware groups: They run on specific route groups of application and can be assigned by listing your middleware class in “$middlewareGroups” array of Kernel class which is located at app/Http/Kernel.php.
Route Middleware: They run on a specific route of application.To assign route middleware on specific routes, you should first assign your middleware a key with class in “$routeMiddleware” array of your app/Http/Kernel.php file.
Once the middleware has been assigned in $routeMiddleware array of the HTTP kernel, you may use the middleware method to assign middleware to a route:

How to create custom middleware in Laravel 5.5?

As we have understood the basics of Middleware in Laravel now its time to create a custom Middleware in Laravel. In this post, we are going to create a custom middleware in Laravel to redirect a visitor to a country subdomain based on location, if the visitor is not coming from the US. Many of e-commerce website do such type of task to personalized user experience based on their location.This middleware is helpful to do so. Lets started, follow the steps with me I am assuming you have already installed Laravel 5.5 on your server, if not please click here to install and configure Laravel 5.5

Step 1: Creating our middleware:

In Laravel you create a new middleware, using the make:middleware Artisan command:
Above command will a new php file named locationRedirect.php in app/Http/Middleware directory. Open your locationRedirect.php file and put below code in that.
Now we need to register and create aliases for our middleware in Kernel.php file so please open app/Http/Kernel.php and add our middleware in the $routeMiddleware array.
Now our custom Middleware locationRedirect is configured and ready to use in our app for specific routes. If you want to use this middleware globally then please uncomment ” // \App\Http\Middleware\locationRedirect::class, // uncomment for global usage” line from $middleware array of app/Http/Kernel.php. For specific route do as below.

Popular posts from this blog

Laravel – Blade Templates

Laravel – Blade Templates Laravel 5.1 introduces the concept of using  Blade , a templating engine to design a unique layout. The layout thus designed can be used by other views, and includes a consistent design and structure. When compared to other templating engines, Blade is unique in the following ways − It does not restrict the developer from using plain PHP code in views. The blade views thus designed, are compiled and cached until they are modified. The complete directory structure of Laravel is shown in the screenshot given here. You can observe that all views are stored in the  resources/views  directory and the default view for Laravel framework is  welcome.blade.php . Please note that other blade templates are also created similarly. Steps for Creating a Blade Template Layout You will have to use the following steps to create a blade template layout − Step 1 Create a layout folder inside the  resources/views  folder. We are...

What is Laravel Framework?

What is Laravel Framework? Laravel is a free, open-source PHP web framework, created by Taylor Otwell and intended for the development of web applications following the model–view–controller (MVC) architectural pattern.

What is routing and how, and what are the different ways to write it?

What is routing and how, and what are the different ways to write it? All Laravel routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by the framework. The routes/web.php file defines routes that are for your web interface. These routes are assigned the web middleware group, which provides features like session state and CSRF protection. The routes in routes/api.php are stateless and are assigned the api middleware group. For most applications, you will begin by defining routes in your routes/web.php file.