【ThinkPHP】5.0 完全开发手册 / 控制器初始化

控制器初始化

如果你的控制器类继承了\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

会输出

init
data