如果你的控制器类继承了\think\Controller类的话,可以定义控制器初始化方法_initialize,在该控制器的方法调用之前首先执行。

例如:

namespace app\index\controller;use think\Controller;class Index extends Controller{    public function _initialize()
    {        echo 'init<br/>';
    }    
    public function hello()
    {        return 'hello';
    }    
    public function data()
    {        return 'data';
    }
}

如果访问
http://localhost/index.php/index/Index/hello

会输出

init
hello

如果访问
http://localhost/index.php/index/Index/data

会输出

initdata


 小秋同学  控制器  2021-03-24 09:49:43    收藏:  赞(896)
学习吧_一个不错的学习网站
本文地址:https://www.qiuhai.com/doc/1606.html 复制链接 如果还有疑问,欢迎加入我们的QQ交流群:887790 加入Q群 易优最新程序下载: 点击下载
QQ在线咨询