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

What is Lumen?

What is Lumen? Lumen  is PHP micro-framework that built on Laravel’s top components.It is created by Taylor Otwell. It is perfect option for building Laravel based micro-services and fast REST API’s. It’s one of the fastest micro-frameworks available. You can install Lumen using composer by running below command 1 composer create - project -- prefer - dist laravel / lumen blog

Laravel – Response

Laravel – Response A web application responds to a user’s request in many ways depending on many parameters. This chapter explains you in detail about responses in Laravel web applications. Basic Response Laravel provides several different ways to return response. Response can be sent either from route or from controller. The basic response that can be sent is simple string as shown in the below sample code. This string will be automatically converted to appropriate HTTP response. Example Step 1  − Add the following code to  app/Http/routes.php  file. app/Http/routes.php 1 2 3 Route :: get ( '/basic_response' , function ( ) {    return 'Hello World' ; } ) ; Step 2  −  Visit  the following URL to test the basic response. 1 http : //localhost:8000/basic_response Step 3  − The output will appear as shown in the following image. Attaching Headers The response can be attached t...

Which template engine Laravel use ?

Which template engine Laravel use ? Laravel uses Blade Templating Engine. Blade is the simple, yet powerful templating engine provided with Laravel. Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. In fact, all Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application. Blade view files use the .blade.php file extension and are typically stored in the resources/views directory.