PHP code example of cxx / tp5-middleware
1. Go to this page and download the library: Download cxx/tp5-middleware library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cxx / tp5-middleware example snippets
return [
// 应用初始化
'app_init' => [],
// 应用开始
'app_begin' => [],
// 模块初始化
'module_init' => [
// !! 此行代码
'Cxx\Tp5Middleware\MiddlewareHandle::moduleInit'
],
// 操作开始执行
'action_begin' => [],
// 视图内容过滤
'view_filter' => [],
// 日志写入
'log_write' => [],
// 应用结束
'app_end' => [
// !! 此行代码
'Cxx\Tp5Middleware\MiddlewareHandle::appEnd'
],
'response_end' => [
// !! 此行代码
'Cxx\Tp5Middleware\MiddlewareHandle::responseEnd'
]
];
return [
Cxx\Tp5Middleware\MiddlewareCommand::class
];
class Middle1
{
/**
* 前置方法
* @param \think\Request $request
*/
public function before($request){}
/**
* 后置方法
* @param \think\Response $response
*/
public function after($response){}
/**
* 响应结束方法
* 在此的任何输出都不会响应给浏览器,参考 thinkphp/library/think/Response.php 128行代码
* @param \think\Request $request
* @param \think\Response $response
*/
public function end($request, $response){}
}
return [
// 全局中间件
'global' => [
Middle1::class
],
// 模块中间件
'module' => [
// 模块名称
'index' => [
Middle1::class
]
],
// 控制器中间件
'controller' => [
// 相应的控制器
Index::class => [
Middle1::class
]
],
];
protected $middleware = [
Middle1::class => ['except' => ['hello'] ],
Middle2::class => ['only' => ['hello'] ],
];