Home >PHP Framework >Laravel >How to create middle key in laravel
Laravel is a popular PHP framework that provides many features and tools to facilitate developers to build high-quality web applications. One notable feature is middleware, which allows developers to perform certain actions before the request reaches its final destination in the application. This article will introduce you to how to create Laravel middleware.
Middleware is a way to intercept routing requests. When the application receives a request, the middleware will perform a series of operations to modify and control the request. Middleware can be used for various operations such as authentication, access control, caching, and more.
Laravel comes with some middleware, such as auth
, csrf
, throttle
, etc. Additionally, developers can create their own middleware to accomplish more specific tasks.
Creating middleware in Laravel is very simple. You only need to execute the following command to generate a new middleware:
php artisan make:middleware MyMiddleware
The above command will be Create a new middleware class named MyMiddleware
in the app/Http/Middleware
directory. This class will contain two methods: handle()
and __construct()
.
handle()
The method is the core of middleware, which defines the operations that need to be performed when the middleware is executed. handle()
The method will receive two parameters: the request object and a callback closure. Typically, middleware modifies the request and passes it to the next handler.
The following is a simple middleware example for recording the response time of a request:
<?php namespace App\Http\Middleware; use Closure; use Illuminate\Support\Facades\Log; class ResponseTimeMiddleware { public function handle($request, Closure $next) { $start_time = microtime(true); $response = $next($request); $end_time = microtime(true); $response_time = round(($end_time - $start_time) * 1000, 2); Log::info("Response time: {$response_time}ms"); return $response; } }
In the above example, we record the start time of the request and calculate it after the request ends Response time. We then log the response time to the application log and return the response.
To use middleware, you need to register it with the application. Laravel provides two methods to register middleware.
Global middleware will run before every request handler. To register a global middleware, open the app/Http/Kernel.php
file and add the class to the $middleware
property.
For example, to register the ResponseTimeMiddleware
middleware we created earlier, we can add the following code to the $middleware
array:
protected $middleware = [ \App\Http\Middleware\ResponseTimeMiddleware::class ];
Route middleware only runs on the specified route. To register a route middleware, add the category name in the $routeMiddleware
attribute in the app/Http/Kernel.php
file.
For example, to register the ResponseTimeMiddleware
middleware we created earlier, we can add the following code to the $routeMiddleware
array:
protected $routeMiddleware = [ 'response_time' => \App\Http\Middleware\ResponseTimeMiddleware::class, ];
In the route file, we can use the category name to specify the middleware to be used:
Route::get('/', function () { return view('welcome'); })->middleware('response_time');
Middleware is a powerful feature of the Laravel framework, which allows developers to execute requests before the application Modify and control requests. In this article, we explored the creation and registration of Laravel middleware, and how to use them in your application. If you haven't used middleware before, I recommend taking some time to learn and start using them.
The above is the detailed content of How to create middle key in laravel. For more information, please follow other related articles on the PHP Chinese website!