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 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM