当前位置:网站首页>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 ";
}边栏推荐
- An intrusion detection model
- Returning to the top of the list, the ID is still weak
- 【锁】Redis锁 处理并发 原子性
- SAP S/4HANA: 一条代码线,许多种选择
- Intelligent operation and maintenance practice: banking business process and single transaction tracking
- The difference between arrow function and ordinary function in JS
- Qt+pcl Chapter 6 point cloud registration ICP Series 2
- 点云重建方法汇总一(PCL-CGAL)
- Raytheon technology rushes to the Beijing stock exchange and plans to raise 540million yuan
- 6.2 normalization 6.2.6 BC normal form (BCNF) 6.2.9 normalization summary
猜你喜欢

MySQL审计插件介绍

Microservice tracking SQL (support Gorm query tracking under isto control)

Phpcms background upload picture button cannot be clicked

Fix the failure of idea global search shortcut (ctrl+shift+f)

A unifying review of deep and shallow anomaly detection
![[stm32-usb-msc problem help] stm32f411ceu6 (Weact) +w25q64+usb-msc flash uses SPI2 to read out only 520kb](/img/ec/fa51b21468708609f998de1b2b84fe.jpg)
[stm32-usb-msc problem help] stm32f411ceu6 (Weact) +w25q64+usb-msc flash uses SPI2 to read out only 520kb

重回榜首的大众,ID依然乏力

Skywalking 6.4 distributed link tracking usage notes

《QT+PCL第六章》点云配准icp系列2

Zhang Chi's class: several types and differences of Six Sigma data
随机推荐
《QT+PCL第九章》点云重建系列2
Description | Huawei cloud store "commodity recommendation list"
skywalking 6.4 分布式链路跟踪 使用笔记
JS中箭头函数和普通函数的区别
点云重建方法汇总一(PCL-CGAL)
Hardware design guide for s32k1xx microcontroller
[stm32-usb-msc problem help] stm32f411ceu6 (Weact) +w25q64+usb-msc flash uses SPI2 to read out only 520kb
phpcms后台上传图片按钮无法点击
微信小程序01-底部导航栏设置
Shopping mall 6.27 to be completed
MySQL审计插件介绍
[cloud trend] new wind direction in June! Cloud store hot list announced
leetcode:329. Longest increasing path in matrix
Go zero actual combat demo (I)
Intelligent operation and maintenance practice: banking business process and single transaction tracking
【天线】【3】CST一些快捷键
"Qt+pcl Chapter 6" point cloud registration ICP Series 6
Tableapi & SQL and MySQL grouping statistics of Flink
Skywalking 6.4 distributed link tracking usage notes
Phpcms background upload picture button cannot be clicked