Simple usage example of laravel framework Middleware

Time:2021-8-26

This paper describes the simple use of laravel framework middleware. Share with you for your reference, as follows:

Laravel has a built-in middleware to verify whether the user has been authenticated. If the user has not been authenticated, the middleware will redirect the user to the login page. Otherwise, if the user has been authenticated, the middleware will allow the request to continue to the next step.

Of course, in addition to authentication, middleware can also be used to handle more tasks. For example, CORS middleware can be used to add appropriate headers (cross domain) to the response of leaving the site; The logging middleware can record all requests to enter the site.

Laravel framework comes with some middleware, including authentication, CSRF protection middleware and so on. All middleware is located in the app / HTTP / Middleware directory.

Whether the intermediate is executed before or after the request depends on the middleware itself. The following middleware will perform some tasks before request processing

<?php
namespace App\Http\Middleware;
use Closure;
class TestMiddle
{
  public function handle($request, Closure $next)
  {
    //Execute action
    if(!$request->session()->has('huser')){
      return redirect("login/index");
    }
    return $next($request);
  }
}

The following middleware will perform its tasks after request processing:

<?php
namespace App\Http\Middleware;
use Closure;
class TestMiddle
{
  public function handle($request, Closure $next)
  {
    $response = $next($request);
    //Execute action
    if(!$request->session()->has('huser')){
      return redirect("login/index");
    }
    return $response;
  }
}

The middleware can create a new corresponding class in the editor or generate it by command


php artisan make:middleware TestMiddle

At this point, the app \ http \ middleware \ directory of laravel will have an additional middleware file of testmiddle.php

At this time, the middleware cannot be used directly. It must be registered in our laravel, as shown below

Just add the app / HTTP / kernel.php class (three attributes, corresponding to which I sometimes use routing)


'TestMiddle' => \App\Http\Middleware\TestMiddle::class,

There are three ways to allocate middleware to routing


Route::get('/',function(){
  return redirect('home/index');
})->middleware('TestMiddle');
Route::group(['middleware' => ['TestMiddle']], function() {
  Route::controller("db","DataBaseController");
});
Route::controller("home","HomeController",['middleware'=>'TestMiddle']);

For more information about laravel, readers who are interested can see the special topics on this site: introduction and advanced tutorial of laravel framework, summary of PHP excellent development framework, introduction to PHP object-oriented programming, introduction to PHP + MySQL database operation, and summary of common database operation skills for PHP

I hope this article will be helpful to your PHP Programming Based on laravel framework.