Table of Contents
Middleware are just like the filter funnel which filters the HTTP request inside any application. Any request before enter into application must needs to pass through the barrier of middleware which application contains.
Middlewares basically used to filter the authenticated request in application. By the help of which we can detect which route is valid or invalid. Apart from this, we have several other examples.
In laravel we have three types of middlewares –
- Global Middleware
- Group Middleware
- Route Middleware
Inside this article, we will see the concept of Global Middleware in Laravel 8.
Let’s get started.
What is Global Middleware?
Global middleware name itself clears that, the middleware which will be applied globally to entire application. All the request inside application must be needs to go with filter funnel i.e middleware and then process next.
By the help of php artisan command, we create Middleware in laravel.
$ php artisan make:middleware <MiddlewareName>
Middlewares will be stored inside /app/Http/Middleware. To register middleware inside application, Kernel.php file will be used which is inside /app/Http. Kernel.php, it’s a class file which contains registration of middlewares.
Installation of Laravel 8 Application
Laravel Installation can be done in two ways.
- Laravel Installer
- By using composer
Laravel Installer
To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.
$ composer global require laravel/installer
This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –
$ laravel
This command will open a command palette of Laravel Installer.
To create ad install laravel project in system,
$ laravel new blog
With the name of blog a laravel project will be created at your specified path.
By using composer
Alternatively, we can also install Laravel by Composer command create-project. If your system doesn’t has Composer Installed, Click here to Install Composer ? Here is the complete command to create a laravel project-
$ composer create-project --prefer-dist laravel/laravel blog
After following these steps we can install a Laravel 8 application into system. To start the development server of Laravel –
$ php artisan serve
This command outputs –
Starting Laravel development server: http://127.0.0.1:8000
Assuming laravel 8 already installed at system.
Example – Global Middleware Implementation
Let’s take an example to understand the concept of global middleware inside laravel application.
- Create a Middleware which checks country, when we open URL.
- US, IN, AFG should be allowed inside application, they can access application routes.
- UK, AUS should be restricted to use or access route.
Create a Middleware
Open project into terminal and run this artisan command.
$ php artisan make:middleware CountryCheck
It will create a file with name CountryCheck.php at /app/Http/Middleware folder.
Open CountryCheck.php file and write this following code into it.
<?php namespace App\Http\Middleware; use Closure; use Illuminate\Http\Request; class CountryCheck { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle(Request $request, Closure $next) { if ($request->country && !in_array($request->country, array("us", "in", "afg"))) { return redirect("noaccess"); } return $next($request); } }
- $request->country is NOT (US, IN, AFG), it will open no access page.
Register Middleware in Application
Open Kernel.php from /app/Http. Search for $middleware and add this.
protected $middleware = [ ... \App\Http\Middleware\CountryCheck::class, ... ];
Create NoAccess Route
Open web.php from /routes and write this route into it.
Route::view("noaccess", "noaccess");
Next, we need to create noaccess.blade.php file.
Create NoAccess Page
Create a file called noaccess.blade.php at /resources/views folder. Open write this simple code into it.
<h3>Sorry! You have no access to open this page.</h3>
Application Testing
Open project to terminal and type the command to start development server
$ php artisan serve
URL – http://127.0.0.1:8000/?country=us
We can access the routes.
URL – http://127.0.0.1:8000/?country=uk
Output:
Sorry! You have no access to open this page.
We hope this article helped you to learn about Concept of Global Middleware in Laravel 8 Tutorial in a very detailed way.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.
Find More on Laravel 8 Articles here
- How to Create Multi Language Website in Laravel 8
- How To Read XML File in Laravel 8 – Example
- How To Upload And Save XML Data in Laravel 8
- Laravel 8 Ajax Post Request Tutorial
- Laravel 8 Authentication using Jetstream with Inertia Js
- Laravel 8 Authentication using Jetstream with Livewire
- Laravel 8 Authentication with Breeze Tutorial
- Laravel 8 Clear Cache of Route, View & Config
- Laravel 8 Cron Job Task Scheduling Tutorial
- Laravel 8 DataTable Ajax Pagination with Search And Sort
- Laravel 8 Firebase Push Notification Tutorial
- Laravel 8 Form Validation Methods
- Laravel 8 Installation Guide – PHP Framework
- Laravel 8 Layouts And Views Complete Guide
- Laravel 8 Routing Tutorial Step by Step Guide
- Laravel 8 Send Mail using Gmail SMTP Server
- Laravel 8 Send Push Notification to Android Using Firebase
- Laravel 8 Send Push Notification to IOS Using Firebase
- Laravel 8 Stub Customization
Hi, I am Sanjay the founder of ONLINE WEB TUTOR. I welcome you all guys here to join us. Here you can find the web development blog articles. You can add more skills in web development courses here.
I am a Web Developer, Motivator, Author & Blogger. Total experience of 7+ years in web development. I also used to take online classes including tech seminars over web development courses. We also handle our premium clients and delivered up to 50+ projects.