当前位置:网站首页>ThinkPHP advanced
ThinkPHP advanced
2022-07-01 15:35:00 【y_ w_ x_ k】
1. access
①:http://localhost/tp2/public/index.php/test/h2
②:http://localhost:8003/Test/h2
2. middleware
① Define middleware
namespace app\middleware;
class Check
{
/**
* Processing requests
*
* @param \think\Request $request
* @param \Closure $next
* @return Response
*/
public function handle($request, \Closure $next)
{
echo " Middleware execution ";
if($request->param("name")=="abc"){
return redirect("test2");
}
// Post middleware , After executing the request method, execute the middleware code
//$next($request) The return value is the message returned to the browser after the request method is executed
//echo "----- Post middleware -----------";
//return $response;
// Front end middleware , Execute relevant code before requesting method execution
return $next($request);
}
// After the request method is executed, return to the browser , Re execution method
public function end(Response $response){
echo "<hr/>";
echo $response->getData()."===".$response->getCode();
}
}
② Register middleware
app/middleware.php
\app\middleware\Check::class,
③ test
public function test1($name="aaa"){
return "test1 Method ".$name;
}
public function test2(){
return "test2 Method ";
}
Routing registration middleware :
class Index extends BaseController
{
public function test3($id){
return "id yes -->".$id;
}
}
Route::get("t3/:id","index/test3")->middleware(\app\middleware\Auth::class);
// Multiple middleware
use app\middleware\Auth;
Route::get("t3/:id","index/test3")->middleware([Auth::class,\app\middleware\Check::class]);
Register middleware by alias :
//config/middleware.php
'alias' => [
"Auth" => \app\middleware\Auth::class,
],
Route::get("t3/:id","index/test3")->middleware(["Auth",\app\middleware\Check::class]);
The middleware passes additional parameters :
Route::get("t3/:id","index/test3")->middleware(["Auth"]," Extra parameters ");
class Auth
{
public function handle($request, \Closure $next,$thParam)
{
echo $thParam;
return $next($request);
}
}
If there is less middleware content, you can use closure :
Route::get("t3/:id","index/test3")->middleware(function ($request, \Closure $next){
echo " Closure middleware ";
return $next($request);
});
Register Middleware in the controller , Execute the middleware when the controller executes
class Index extends BaseController
{
protected $middleware=[Auth2::class];
}
// The individual methods of the controller execute the middleware
class Index extends BaseController
{
protected $middleware=[
Auth2::class=>[
"only"=>['test2'],
"except"=>["test3"]
]
];
....
}
Middleware through $request Pass parameters to the controller method
class Auth2
{
public function handle($request, \Closure $next)
{
$request->myName=" Farewell my concubine ";
return $next($request);
}
}
public function test2(Request $req){
echo $req->myName;
return "test2 Method ";
}
边栏推荐
- Junda technology - wechat cloud monitoring scheme for multiple precision air conditioners
- 【目标跟踪】|模板更新 时间上下文信息(UpdateNet)《Learning the Model Update for Siamese Trackers》
- 微信小程序03-文字一左一右显示,行内块元素居中
- Raytheon technology rushes to the Beijing stock exchange and plans to raise 540million yuan
- 榨汁机UL982测试项目有哪些
- ABAP-调用Restful API
- 精益六西格玛项目辅导咨询:集中辅导和点对点辅导两种方式
- 厦门灌口镇田头村特色农产品 甜头村特色农产品蚂蚁新村7.1答案
- 做空蔚来的灰熊,以“碰瓷”中概股为生?
- 微服务追踪SQL(支持Isto管控下的gorm查询追踪)
猜你喜欢
Opencv Learning Notes 6 -- image mosaic
《QT+PCL第九章》点云重建系列2
Qt+pcl Chapter 9 point cloud reconstruction Series 2
【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(三)
[target tracking] | template update time context information (updatenet) "learning the model update for Siamese trackers"
Survey of intrusion detection systems:techniques, datasets and challenges
S32K1xx 微控制器的硬件設計指南
微信小程序02-轮播图实现与图片点击跳转
Tiantou village, Guankou Town, Xiamen special agricultural products Tiantou Village special agricultural products ant new village 7.1 answer
【显存优化】深度学习显存优化方法
随机推荐
Summary of point cloud reconstruction methods I (pcl-cgal)
Hardware design guide for s32k1xx microcontroller
关于用 ABAP 代码手动触发 SAP CRM organization Model 自动决定的研究
leetcode:329. Longest increasing path in matrix
【STM32-USB-MSC问题求助】STM32F411CEU6 (WeAct)+w25q64+USB-MSC Flash用SPI2 读出容量只有520KB
《QT+PCL第六章》点云配准icp系列2
Raytheon technology rushes to the Beijing stock exchange and plans to raise 540million yuan
【锁】Redis锁 处理并发 原子性
Deep operator overloading (2)
Implementation of wechat web page subscription message
[cloud trend] new wind direction in June! Cloud store hot list announced
微信小程序02-轮播图实现与图片点击跳转
《QT+PCL第六章》点云配准icp系列5
Wechat applet 03 - text is displayed from left to right, and the block elements in the line are centered
微信网页订阅消息实现
异常检测中的浅层模型与深度学习模型综述(A Unifying Review of Deep and Shallow Anomaly Detection)
[target tracking] | template update time context information (updatenet) "learning the model update for Siamese trackers"
Introduction to MySQL audit plug-in
Flink 系例 之 TableAPI & SQL 与 MYSQL 数据查询
Qt+pcl Chapter 6 point cloud registration ICP series 4