当前位置:网站首页>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方法";
}
边栏推荐
猜你喜欢
Raytheon technology rushes to the Beijing stock exchange and plans to raise 540million yuan
点云重建方法汇总一(PCL-CGAL)
MySQL审计插件介绍
[one day learning awk] conditions and cycles
A unifying review of deep and shallow anomaly detection
S32K1xx 微控制器的硬件设计指南
Opencv learning note 4 -- bank card number recognition
如何实现时钟信号分频?
微信小程序02-轮播图实现与图片点击跳转
How to realize clock signal frequency division?
随机推荐
TS reports an error don't use 'object' as a type The `object` type is currently hard to use
[leetcode] 16. The sum of the nearest three numbers
openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题
Zhang Chi Consulting: household appliance enterprises use Six Sigma projects to reduce customers' unreasonable return cases
STM32F411 SPI2输出错误,PB15无脉冲调试记录【最后发现PB15与PB14短路】
leetcode:329. Longest increasing path in matrix
Connect the ABAP on premises system to the central inspection system for custom code migration
Opencv learning note 4 -- bank card number recognition
Flink 系例 之 TableAPI & SQL 与 MYSQL 插入数据
What is the relationship between network speed, broadband, bandwidth and traffic?
智能运维实战:银行业务流程及单笔交易追踪
Hidden rules of the workplace that must be understood before 30
Detailed explanation of ArrayList expansion, expansion principle [easy to understand]
《QT+PCL第六章》点云配准icp系列6
Wechat applet 01 bottom navigation bar settings
【STM32-USB-MSC问题求助】STM32F411CEU6 (WeAct)+w25q64+USB-MSC Flash用SPI2 读出容量只有520KB
微服务追踪SQL(支持Isto管控下的gorm查询追踪)
Fix the failure of idea global search shortcut (ctrl+shift+f)
将ABAP On-Premises系统连接到中央检查系统以进行自定义代码迁移
Wechat applet 03 - text is displayed from left to right, and the block elements in the line are centered