Note Thinkphp6 XI: Configuration middleware

Command to create: php think make: middleware Check
if the file does not exist, manually create the app \ middleware \ Check.php
middleware Check.php Code

<?php
declare (strict_types = 1);

namespace app\middleware;

class Check
{
/**
* 处理请求
*
* @param \think\Request $request
* @param \Closure $next
* @return Response
*/
    public function handle($request, \Closure $next)
    {
        echo 'middleware';
        return $next($request);
    }
}        

 

One: Middleware call
1: A route calls
admin / route / route.php

<?php
use think\facade\Route;
Route::get('/test', 'admin/index/test')->middleware(\app\middleware\Check::class);

 

2: an application calls
/app/admin/middleware.php

? < PHP
 // This is an automatically generated middleware definition file 
return [ 
App \ middleware \ the Check :: class , 
];

 

3: Global call
/config/middleware.php add the following code

'middleware' => [
    app\middleware\Check::class,
],

 


II: Middleware alias
config / middleware.php define aliases

<? PHP
 // intermediate configuration 
return [
     // alias or packets 
    'Alias' => [
        'ccheck' => App \ Middleware \ :: the Check class , 
    ] ,
     // priority setting, this array will middleware the order of precedence in the array 
    'priority' => [], 
];

 

Application calls alias
app \ admin \ middleware.php

? < PHP
 // this is a system of middleware definition file generated automatically 
return [
     'ccheck', 
];

 


Scenario:
access logs, login detection, ip blacklist, SQL injection,

Guess you like

Origin www.cnblogs.com/wesky/p/12660972.html