当前位置:网站首页>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 ";
}边栏推荐
- Hardware design guide for s32k1xx microcontroller
- 远程办公经验?来一场自问自答的介绍吧~ | 社区征文
- 微信小程序03-文字一左一右显示,行内块元素居中
- 【目标跟踪】|模板更新 时间上下文信息(UpdateNet)《Learning the Model Update for Siamese Trackers》
- Tableapi & SQL and MySQL data query of Flink
- Summary of week 22-06-26
- 入侵检测模型(An Intrusion-Detection Model)
- How to realize clock signal frequency division?
- leetcode:329. Longest increasing path in matrix
- The solution to turn the newly created XML file into a common file in idea
猜你喜欢
![Stm32f411 SPI2 output error, pb15 has no pulse debugging record [finally, pb15 and pb14 were found to be short circuited]](/img/ea/8c9f716717bc08f2e563c577738ec8.png)
Stm32f411 SPI2 output error, pb15 has no pulse debugging record [finally, pb15 and pb14 were found to be short circuited]

【一天学awk】条件与循环

OpenSSL client programming: SSL session failure caused by an insignificant function

微信小程序01-底部导航栏设置

采集数据工具推荐,以及采集数据列表详细图解流程
![[one day learning awk] conditions and cycles](/img/e6/c96a4fd6ced9b492e70a06004f5159.png)
[one day learning awk] conditions and cycles

微信公众号订阅消息 wx-open-subscribe 的实现及闭坑指南

微信小程序02-轮播图实现与图片点击跳转

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

【显存优化】深度学习显存优化方法
随机推荐
微服务追踪SQL(支持Isto管控下的gorm查询追踪)
A unifying review of deep and shallow anomaly detection
S32K1xx 微控制器的硬件设计指南
Survey of intrusion detection systems:techniques, datasets and challenges
智能运维实战:银行业务流程及单笔交易追踪
Go zero actual combat demo (I)
Tableapi & SQL and Kafka message acquisition of Flink example
[STM32 learning] w25qxx automatic judgment capacity detection based on STM32 USB storage device
Junda technology indoor air environment monitoring terminal PM2.5, temperature and humidity TVOC and other multi parameter monitoring
Flink 系例 之 TableAPI & SQL 与 MYSQL 插入数据
【STM32-USB-MSC问题求助】STM32F411CEU6 (WeAct)+w25q64+USB-MSC Flash用SPI2 读出容量只有520KB
Task.Run(), Task.Factory.StartNew() 和 New Task() 的行为不一致分析
Opencv learning note 4 -- bank card number recognition
Research on manually triggering automatic decision of SAP CRM organization model with ABAP code
Intelligent operation and maintenance practice: banking business process and single transaction tracking
"Qt+pcl Chapter 6" point cloud registration ICP Series 6
SAP CRM organization Model(组织架构模型)自动决定的逻辑分析
[Cloudera][ImpalaJDBCDriver](500164)Error initialized or created transport for authentication
《QT+PCL第六章》点云配准icp系列6
常见健身器材EN ISO 20957认证标准有哪些