thinkPHP6 中间件传参

可以通过给请求对象赋值的方式传参给控制器(或者其它地方),例如

中间件传参 : 前置中间件

传:

复制代码
 1 <?php
 2 
 3 namespace app\http\middleware;
 4 
 5 class Hello
 6 {
 7     public function handle($request, \Closure $next)
 8     {
 9         $request->hello = 'ThinkPHP';
10         
11         return $next($request);
12     }
13 }
复制代码

后置中间件:

复制代码
 1 <?php
 2 declare (strict_types = 1);
 3 
 4 namespace app\index\middleware; 6 use think\facade\Session;
 7 class Show
 8 {
 9     public function handle($request, \Closure $next)
10     {
11         $request->rows = $rows;
12 
13         $response = $next($request);
14 
15         $url = request()->controller().'/'.request()->action();
16         if (!Session::has('member_name')){
17             $currenturl = ['Product/product'];
18             if (in_array($url,$currenturl)){
19                 return redirect((string)url('Login/login'));
20             }
21         }
22         return $response;
23     }
24 }
复制代码

 

接:

复制代码
1 public function index(Request $request)
2 {
3     return $request->hello; // ThinkPHP
4 }
5 ----------------------------------------------------------------------------
6 public function index()
7 {
8     return request()->hello; // ThinkPHP
9 }
复制代码

 

posted @   旅行者无声  阅读(1116)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
点击右上角即可分享
微信分享提示