HTTP Middleware
- Brief introduction
- Build middleware
- Register middleware
- can terminate middleware
Brief introduction
The HTTP middleware provides a convenient mechanism to filter HTTP requests into the application, for example, Laravel contains a middleware to verify user authentication by default, and if the user is not authenticated, the middleware directs the user to the login page, however, if the user is authenticated, The middleware will allow this request to move further forward.
Of course, in addition to authentication, middleware can also be used to perform a variety of tasks, CORS Middleware is responsible for all the response to leave the program to join the appropriate response header, a log middleware can record all incoming application requests. The Laravel framework has built in some middleware, including maintenance, authentication, CSRF protection, and so on. All middleware is located app/Http/Middleware
within the directory.
Build middleware
To create a new middleware, you can use make:middleware
this Artisan command:
php artisan make:middleware OldMiddleware
This command will be built into the app/Http/Middleware
directory OldMiddleware
with a class named. In this middleware we allow only more 年龄
than 200 to access the route, otherwise we will re-orient the user to the URI of the "home".
<?php namespace App\Http\Middleware;use Closure;class OldMiddleware { /** * Run the request filter. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { if ($request->input(‘age‘) < 200) { return redirect(‘home‘); } return $next($request); }}
As you can see, if it is 年龄
less than 200
, the middleware will return HTTP redirection to the client, otherwise the request will be further passed to the application. You can $request
$next
pass requests to deeper applications (allowing skipping middleware) just by invoking the method with the HTTP request before actually touching the application, it is best to layer through many middleware, each layer can check the request, or even completely deny the request.
before/
AfterMiddleware
Specifying a middleware before and after a request depends on the middleware itself. This middleware can perform some pre -operation before the request:
<?php namespace App\Http\Middleware;use Closure;class BeforeMiddleware implements Middleware { public function handle($request, Closure $next) { // Perform action return $next($request); }}
The middleware can then perform some post -operation after the request:
<?php namespace App\Http\Middleware;use Closure;class AfterMiddleware implements Middleware { public function handle($request, Closure $next) { $response = $next($request); // Perform action return $response; }}
Register Middleware Global Middleware
If you want the middleware to be executed by all HTTP requests, simply add the middleware class to app/Http/Kernel.php
the $middleware
list of property listings.
Assigning Middleware to Routing
If you want to assign the middleware to a particular route, you have to first configure the middleware with app/Http/Kernel.php
a key value, by default, the attributes in this file $routeMiddleware
already contain the middleware currently configured by Laravel, and you only need to add a set of custom key values to the list. Once the middleware is defined within the HTTP kernel file, you can use the key values within the routing options middleware
to assign:
Route::get(‘admin/profile‘, [‘middleware‘ => ‘auth‘, function(){ //}]);
can terminate middleware
Sometimes the middleware needs to be executed after the HTTP response has been sent to the client, for example, the Laravel built-in "session" middleware, where the session data is saved after the response has been sent to the client. To do this, you need to define the middleware as "terminating".
use Closure;use Illuminate\Contracts\Routing\TerminableMiddleware;class StartSession implements TerminableMiddleware { public function handle($request, Closure $next) { return $next($request); } public function terminate($request, $response) { // Store the session data... }}
As you can see, define a method In addition handle
to defining the method TerminableMiddleware
terminate
. This method receives the request and the response. Once you have defined the Terminable middleware, you need to add it to the list of global middleware listings for the HTTP kernel file.
Laravel5.1 Learning Note 3 HTTP middleware