1、thinkphp访问路径拆解
路径:http://www.yftest.com / index.php / admin / test / index
域名· 入口文件 模块 controller action
2、不同的controller之间的访问
不同controller之间方法的访问有三个方法
方法一:使用命名空间
<?php namespace app\admin\controller; use think\Controller; class Index extends Controller{ public function Index() { //方法一 $test = new \app\admin\controller\Test(); return $test->index(); //也可写作以下写法 return call_user_func([new \app\admin\controller\Test(), ‘index‘]); } }
方法二:使用use的方式
<?php namespace app\admin\controller; use think\Controller; use app\admin\controller\Test; class Index extends Controller{ public function Index() { //方法二 $test = new Test(); return $test->index(); //也可写作以下写法 return call_user_func([new Test(), ‘index‘]); } }
方法三:使用thinkphp的内置方法
<?php namespace app\admin\controller; use think\Controller; class Index extends Controller{ public function Index() { //方法三 $test = controller(‘index/Index‘); return $test->index(); return call_user_func([controller(‘index/Index‘), ‘index‘]); //以上表示的是在index模块下的Index控制器里的index这个方法,直接实例化好的 } }
3、调用当前控制器里的方法
<?php namespace app\admin\controller; use think\Controller; class Index extends Controller{ public function Index() { //方法一 常用 return $this->check(); // //方法二 return self::check(); // //方法三 return Index::check(); // //方法四 常用 return action(‘check‘); } public function check() { return ‘this is check page‘; } }
如果是跨模块,那么可以使用第四种方法,如下
<?php namespace app\admin\controller; use think\Controller; class Index extends Controller{ public function Index() { //表示模块/controller/action return action(‘index/Index/index‘); } }
原文:https://www.cnblogs.com/rickyctbu/p/11520577.html