当前位置:网站首页>ThinkPHP进阶
ThinkPHP进阶
2022-07-01 15:24:00 【y_w_x_k】
1.访问方式
①:http://localhost/tp2/public/index.php/test/h2
②:http://localhost:8003/Test/h2
2.中间件
①定义中间件
namespace app\middleware;
class Check
{
/**
* 处理请求
*
* @param \think\Request $request
* @param \Closure $next
* @return Response
*/
public function handle($request, \Closure $next)
{
echo "中间件执行";
if($request->param("name")=="abc"){
return redirect("test2");
}
//后置中间件,执行完请求方法之后执行中间件代码
//$next($request)返回值就是请求方法执行后返回给浏览器的报文
//echo "-----后置中间件-----------";
//return $response;
//前置中间件,在请求方法执行之前执行相关代码
return $next($request);
}
//请求方法执行完返回到浏览器后,再执行的方法
public function end(Response $response){
echo "<hr/>";
echo $response->getData()."===".$response->getCode();
}
}②注册中间件
app/middleware.php
\app\middleware\Check::class,③测试
public function test1($name="aaa"){
return "test1方法".$name;
}
public function test2(){
return "test2方法";
}路由方式注册中间件:
class Index extends BaseController
{
public function test3($id){
return "id是-->".$id;
}
}Route::get("t3/:id","index/test3")->middleware(\app\middleware\Auth::class);//多个中间件
use app\middleware\Auth;
Route::get("t3/:id","index/test3")->middleware([Auth::class,\app\middleware\Check::class]);别名方式注册中间件:
//config/middleware.php
'alias' => [
"Auth" => \app\middleware\Auth::class,
],Route::get("t3/:id","index/test3")->middleware(["Auth",\app\middleware\Check::class]);中间件传递额外参数:
Route::get("t3/:id","index/test3")->middleware(["Auth"],"额外参数");class Auth
{
public function handle($request, \Closure $next,$thParam)
{
echo $thParam;
return $next($request);
}
}如果中间件内容较少可以使用闭包方式:
Route::get("t3/:id","index/test3")->middleware(function ($request, \Closure $next){
echo "闭包中间件";
return $next($request);
});在控制器里注册中间件,当控制器执行的时候执行中间件
class Index extends BaseController
{
protected $middleware=[Auth2::class];
}//控制器的个别方法执行中间件
class Index extends BaseController
{
protected $middleware=[
Auth2::class=>[
"only"=>['test2'],
"except"=>["test3"]
]
];
....
}中间件通过$request传递参数到控制器方法
class Auth2
{
public function handle($request, \Closure $next)
{
$request->myName="霸王别姬";
return $next($request);
}
}public function test2(Request $req){
echo $req->myName;
return "test2方法";
}边栏推荐
- IDEA全局搜索快捷键(ctrl+shift+F)失效修复
- 关于用 ABAP 代码手动触发 SAP CRM organization Model 自动决定的研究
- Opencv Learning Notes 6 -- image mosaic
- 22-06-26周总结
- Go zero actual combat demo (I)
- 《QT+PCL第六章》点云配准icp系列6
- Digital transformation: data visualization enables sales management
- 《性能之巅第2版》阅读笔记(五)--file-system监测
- 【目标跟踪】|STARK
- 【LeetCode】16、最接近的三数之和
猜你喜欢

Skywalking 6.4 distributed link tracking usage notes

《QT+PCL第九章》点云重建系列2

Raytheon technology rushes to the Beijing stock exchange and plans to raise 540million yuan

MySQL 服务正在启动 MySQL 服务无法启动解决途径

An intrusion detection model

《性能之巅第2版》阅读笔记(五)--file-system监测

Wechat applet 02 - Implementation of rotation map and picture click jump

竣达技术丨多台精密空调微信云监控方案
Implementation of deploying redis sentry in k8s

Lean Six Sigma project counseling: centralized counseling and point-to-point counseling
随机推荐
SAP CRM organization Model(组织架构模型)自动决定的逻辑分析
Fix the failure of idea global search shortcut (ctrl+shift+f)
Intelligent operation and maintenance practice: banking business process and single transaction tracking
The markdown editor uses basic syntax
微信小程序01-底部导航栏设置
Demand prioritization method based on value quantification
厦门灌口镇田头村特色农产品 甜头村特色农产品蚂蚁新村7.1答案
Filter & (login interception)
Recommendation of data acquisition tools and detailed graphic process of data acquisition list
Tableapi & SQL and Kafka message acquisition of Flink example
It's settled! 2022 Hainan secondary cost engineer examination time is determined! The registration channel has been opened!
skywalking 6.4 分布式链路跟踪 使用笔记
常见健身器材EN ISO 20957认证标准有哪些
[leetcode] 16. The sum of the nearest three numbers
SAP S/4HANA: 一条代码线,许多种选择
【一天学awk】条件与循环
"Qt+pcl Chapter 6" point cloud registration ICP Series 6
使用swiper制作手机端轮播图
Tiantou village, Guankou Town, Xiamen special agricultural products Tiantou Village special agricultural products ant new village 7.1 answer
竣达技术丨室内空气环境监测终端 pm2.5、温湿度TVOC等多参数监测