当前位置:网站首页>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方法";
}边栏推荐
- Wechat applet 02 - Implementation of rotation map and picture click jump
- Flink 系例 之 TableAPI & SQL 与 Kafka 消息插入
- SAP CRM organization Model(组织架构模型)自动决定的逻辑分析
- skywalking 6.4 分布式链路跟踪 使用笔记
- MySQL 服务正在启动 MySQL 服务无法启动解决途径
- Lean Six Sigma project counseling: centralized counseling and point-to-point counseling
- Skywalking 6.4 distributed link tracking usage notes
- Digital transformation: data visualization enables sales management
- 【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(三)
- 贝联珠贯加入龙蜥社区,共同促进碳中和
猜你喜欢

【一天学awk】函数与自定义函数

The data in the database table recursively forms a closed-loop data. How can we get these data

【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(三)

【目标跟踪】|STARK

Junda technology indoor air environment monitoring terminal PM2.5, temperature and humidity TVOC and other multi parameter monitoring

How to realize clock signal frequency division?
![opencv学习笔记六--图像特征[harris+SIFT]+特征匹配](/img/50/5c8adacea78e470c255070c8621ddd.png)
opencv学习笔记六--图像特征[harris+SIFT]+特征匹配

异常检测中的浅层模型与深度学习模型综述(A Unifying Review of Deep and Shallow Anomaly Detection)

Qt+pcl Chapter 9 point cloud reconstruction Series 2

Fix the failure of idea global search shortcut (ctrl+shift+f)
随机推荐
Apk signature principle
采集数据工具推荐,以及采集数据列表详细图解流程
Digital transformation: data visualization enables sales management
Filter &(登录拦截)
Introduction to MySQL audit plug-in
Opencv learning note 4 -- bank card number recognition
Configuration of ZABBIX API and PHP
Is JPMorgan futures safe to open an account? What is the account opening method of JPMorgan futures company?
[cloud trend] new wind direction in June! Cloud store hot list announced
Filter & (login interception)
【天线】【3】CST一些快捷键
opencv学习笔记六--图像特征[harris+SIFT]+特征匹配
Research on manually triggering automatic decision of SAP CRM organization model with ABAP code
[advanced ROS] lesson 5 TF coordinate transformation in ROS
使用 csv 导入的方式在 SAP S/4HANA 里创建 employee 数据
k8s部署redis哨兵的实现
【一天学awk】条件与循环
Qt+pcl Chapter 6 point cloud registration ICP series 3
智能运维实战:银行业务流程及单笔交易追踪
《QT+PCL第六章》点云配准icp系列4