CI2框架中间件实现
class MY_Controller extends Codeigniter\Middleware\CM_Controller
{
}
class MY_Loader extends Codeigniter\Middleware\CM_Loader
{
}
class throttle_middleware extends Codeigniter\Middleware\Abstracts\CI_Middleware
{
public function handle(\Closure $next, $maxAttempts = 60, $decayMinutes = 1)
{
return $next();
}
}
class Test extends MY_Controller
{
public function init()
{
// 中间件
$this->middleware('csrf_middleware');
// 带参数的中间件
$this->middleware('throttle_middleware', [
'params' => [60,10],
'except' => ['index']
]);
// 匿名中间件
$this->middleware(function($next){
if(true){
return ['error_code' => 1, 'error_msg' => '我拦住你了'];
}
return $next();
}, [
'only' => ['index']
]);
// 带后置操作的匿名中间件
$this->middleware(function($next){
echo 'this is test route.';
$response = $next();
echo "后置中间件";
return $response;
}, [
'except' => ['index'],
]);
}
public function index()
{
echo "hello world";
}
public function test1()
{
echo "test1";
}
public function test2()
{
echo "test2";
}
public function notify()
{
echo date('Y-m-d H:i:s');
}
}