当前位置:网站首页>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方法";
}边栏推荐
- idea中新建的XML文件变成普通文件的解决方法.
- 【目标跟踪】|模板更新 时间上下文信息(UpdateNet)《Learning the Model Update for Siamese Trackers》
- These three online PS tools should be tried
- [Cloudera][ImpalaJDBCDriver](500164)Error initialized or created transport for authentication
- Build MySQL master-slave server under Ubuntu 14.04
- Qt+pcl Chapter 6 point cloud registration ICP Series 5
- leetcode:329. 矩阵中的最长递增路径
- [antenna] [3] some shortcut keys of CST
- Tiantou village, Guankou Town, Xiamen special agricultural products Tiantou Village special agricultural products ant new village 7.1 answer
- TS reports an error don't use 'object' as a type The `object` type is currently hard to use
猜你喜欢

Basic use process of cmake

采集数据工具推荐,以及采集数据列表详细图解流程

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

STM32F411 SPI2输出错误,PB15无脉冲调试记录【最后发现PB15与PB14短路】
![[leetcode] 16. The sum of the nearest three numbers](/img/60/6a68333d6e543c601e6ed586b830d0.png)
[leetcode] 16. The sum of the nearest three numbers
Implementation of deploying redis sentry in k8s
Sort out the four commonly used sorting functions in SQL

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

Returning to the top of the list, the ID is still weak

Recommendation of data acquisition tools and detailed graphic process of data acquisition list
随机推荐
Flink 系例 之 TableAPI & SQL 与 MYSQL 数据查询
Hardware design guide for s32k1xx microcontroller
【锁】Redis锁 处理并发 原子性
点云重建方法汇总一(PCL-CGAL)
微信小程序02-轮播图实现与图片点击跳转
The difference between arrow function and ordinary function in JS
异常检测中的浅层模型与深度学习模型综述(A Unifying Review of Deep and Shallow Anomaly Detection)
厦门灌口镇田头村特色农产品 甜头村特色农产品蚂蚁新村7.1答案
phpcms后台上传图片按钮无法点击
常见健身器材EN ISO 20957认证标准有哪些
微服务追踪SQL(支持Isto管控下的gorm查询追踪)
如何实现时钟信号分频?
智能运维实战:银行业务流程及单笔交易追踪
Qt+pcl Chapter 6 point cloud registration ICP Series 2
skywalking 6.4 分布式链路跟踪 使用笔记
做空蔚来的灰熊,以“碰瓷”中概股为生?
微信小程序03-文字一左一右显示,行内块元素居中
采集数据工具推荐,以及采集数据列表详细图解流程
Survey of intrusion detection systems:techniques, datasets and challenges
Connect the ABAP on premises system to the central inspection system for custom code migration