当前位置:网站首页>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 ";
}
边栏推荐
- MySQL backup and restore single database and single table
- The difference between arrow function and ordinary function in JS
- "Qt+pcl Chapter 6" point cloud registration ICP Series 6
- [Cloudera][ImpalaJDBCDriver](500164)Error initialized or created transport for authentication
- [lock] redis lock handles concurrency atomicity
- MySQL高级篇4
- [300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (III)
- Is JPMorgan futures safe to open an account? What is the account opening method of JPMorgan futures company?
- STM32ADC模拟/数字转换详解
- 【云动向】6月上云新风向!云商店热榜揭晓
猜你喜欢
Don't ask me again why MySQL hasn't left the index? For these reasons, I'll tell you all
Task.Run(), Task.Factory.StartNew() 和 New Task() 的行为不一致分析
Introduction to MySQL audit plug-in
JS中箭头函数和普通函数的区别
点云重建方法汇总一(PCL-CGAL)
MySQL 服务正在启动 MySQL 服务无法启动解决途径
Tiantou village, Guankou Town, Xiamen special agricultural products Tiantou Village special agricultural products ant new village 7.1 answer
Survey of intrusion detection systems:techniques, datasets and challenges
Reading notes of top performance version 2 (V) -- file system monitoring
Phpcms background upload picture button cannot be clicked
随机推荐
微信小程序01-底部导航栏设置
Opencv Learning Notes 6 -- image mosaic
Introduction to MySQL audit plug-in
ABAP-屏幕切换时,刷新上一个屏幕
Beilianzhuguan joined the dragon lizard community to jointly promote carbon neutralization
Zhang Chi's class: several types and differences of Six Sigma data
Photoshop插件-HDR(二)-脚本开发-PS插件
[one day learning awk] conditions and cycles
《QT+PCL第六章》点云配准icp系列5
入侵检测模型(An Intrusion-Detection Model)
将ABAP On-Premises系统连接到中央检查系统以进行自定义代码迁移
A unifying review of deep and shallow anomaly detection
leetcode:329. Longest increasing path in matrix
微信小程序02-轮播图实现与图片点击跳转
Reading notes of top performance version 2 (V) -- file system monitoring
leetcode:329. 矩阵中的最长递增路径
Stm32f4-tft-spi timing logic analyzer commissioning record
Guide de conception matérielle du microcontrôleur s32k1xx
Summary of week 22-06-26
Lean Six Sigma project counseling: centralized counseling and point-to-point counseling