结论:thinkphp6.0控制器中间件,在initialize()之后执行
测试:
1、首先创建一个tp6的新项目
composer create-project topthink/think tp
2、安装多应用模式扩展
composer require topthink/think-multi-app
3、创建一个新应用
php think build index
4、创建一个中间件
php think make:middleware index@Test
5、在app\index\middleware\Test.php中添加测试代码
<?php declare (strict_types = 1); namespace app\index\middleware; use think\Response; class Test { /** * 处理请求 * * @param \think\Request $request * @param \Closure $next * @return Response */ public function handle($request, \Closure $next) { $request->test_name = 'hello world'; dump('中间件'); return $next($request); } }
6、在app\index\controller\Index.php中添加测试代码
<?php declare (strict_types = 1); namespace app\index\controller; use app\BaseController; use app\index\middleware\Test; use app\Request; use think\App; class Index extends BaseController { protected $middleware = [ Test::class, ]; public function initialize() { parent::initialize(); dump('initialize:' . $this->request->test_name); } protected function getTestName() { dump($this->request->test_name); } public function index(Request $request) { $this->getTestName(); echo 'index'; } }
7、在项目根目录下面执行:
php think run
8、在浏览器中访问http://127.0.0.1:8000
看到如下结果:
由此可知tp6的控制器中间件,在initialize()之后执行,在initialize()中是获取不到中间件中设置的test_name的
声明:
① 凡本网所有原创文章及图片、图表的版权均属WX部落所有,如需转载,需注明“信息来源:WX部落”,并且添加本文地址:http://wxbuluo.com/index/article/38.html
② 凡本网注明“来源:XXX(非WX部落)”的文字及图片内容,均转载自其他媒体,版权归原媒体及作者所有。转载目的在于传递更多的资讯,并不代表本网赞同其观点和对其真实性负责。如有侵权,请联系删除。联系方式:296720094@qq.com
③ 本网站的资源部分内容来源于网络,仅供大家学习与参考,如有侵权,请联系站长296720094@qq.com进行删除处理。
④ 部分项目课程具有时效性,如发布时间较长请搜索相关课程选择发布时间最近的查看。
⑤ 部分具有时效性的项目课程文章,我们会逐步转移到免费类分类开放下载。同时免费分类链接失效不补!
⑥ 本站资源大多存储在云盘,如发现链接失效,请联系我们我们会第一时间更新。
2023 © wxbuluo.com ALL Rights Reserved. 鲁ICP备15005397号-1