📖 Scenario: You are building a Laravel web application that needs to log every incoming HTTP request's URL for monitoring purposes.To do this efficiently, you will create a global middleware that runs on every request and logs the URL.
🎯 Goal: Create a global middleware in Laravel that logs the URL of every incoming request.Register this middleware globally so it runs on all routes automatically.
📋 What You'll Learn
Create a middleware class named LogRequestUrl in the app/Http/Middleware directory.
In the middleware, log the current request URL using Laravel's Log facade.
Register the LogRequestUrl middleware globally in the app/Http/Kernel.php file.
Verify the middleware runs on every request by checking the log file.
💡 Why This Matters
🌍 Real World
Global middleware is useful for tasks that must happen on every request, such as logging, security checks, or setting locale preferences.
💼 Career
Understanding global middleware registration and creation is essential for Laravel developers to implement cross-cutting concerns efficiently.
Progress0 / 4 steps
1
Create the LogRequestUrl middleware class
Create a middleware class named LogRequestUrl inside the app/Http/Middleware directory. The class should have a handle method that accepts $request and $next parameters and returns $next($request).
Laravel
Hint
Use the handle method with parameters Request $request and Closure $next. Return $next($request) at the end.
2
Add logging of the request URL in the middleware
Inside the handle method of LogRequestUrl, add a line to log the current request URL using \Log::info($request->url()) before returning $next($request). Make sure to import the Log facade with use Illuminate\Support\Facades\Log;.
Laravel
Hint
Import the Log facade and call Log::info() with the request URL inside the handle method.
3
Register the middleware globally in Kernel.php
Open the app/Http/Kernel.php file. Add the \App\Http\Middleware\LogRequestUrl::class entry to the $middleware array property to register it as global middleware.
Laravel
Hint
In the Kernel class, add \App\Http\Middleware\LogRequestUrl::class inside the $middleware array.
4
Verify the middleware logs URLs on every request
Make a request to any route in your Laravel app. Check the storage/logs/laravel.log file to confirm that the requested URL is logged by the LogRequestUrl middleware.
Laravel
Hint
After registering the middleware globally, any request will trigger the logging. Check the log file to confirm.
Practice
(1/5)
1. What is the main purpose of global middleware in a Laravel application?
easy
A. To run code on every HTTP request to the application
B. To handle database migrations automatically
C. To define routes for the application
D. To manage user sessions only on login
Solution
Step 1: Understand what middleware does
Middleware acts as a filter or layer that runs code before or after a request is handled.
Step 2: Identify the role of global middleware
Global middleware runs on every HTTP request, applying tasks like authentication or logging universally.
Final Answer:
To run code on every HTTP request to the application -> Option A
Quick Check:
Global middleware = runs on every request [OK]
Hint: Global middleware affects all requests, not just some [OK]
Common Mistakes:
Confusing middleware with routing
Thinking middleware only runs on login
Assuming middleware manages database
2. Which file in a Laravel project is used to register global middleware?
easy
A. config/app.php
B. routes/web.php
C. app/Http/Kernel.php
D. app/Providers/AppServiceProvider.php
Solution
Step 1: Recall where middleware is registered
Middleware is registered in the Kernel class which manages HTTP middleware stacks.
Step 2: Identify the correct Kernel file
In Laravel, the HTTP Kernel is located at app/Http/Kernel.php where global middleware is listed in the $middleware array.
Final Answer:
app/Http/Kernel.php -> Option C
Quick Check:
Global middleware registered in Kernel.php [OK]
Hint: Middleware registration is in Kernel.php, not routes or config [OK]
Common Mistakes:
Choosing routes/web.php which defines routes only
Confusing config/app.php with middleware registration
Selecting service providers which are for services