TP6获取控制器名,方法名

以下调用方法自选一种,哪种好玩你就用哪种。

第一种:助手函数调用
Request()->controller() //获取控制器名
Request()->action() //获取方法名
第二种:门面函数facade的静态调用
\think\facade\Request::action()
\think\facade\Request::controller()
第三种:app类的静态调用,注意此App为 use think\facade\App;
use think\facade\App;
use think\Facade;
 
(App::class)::make('request')->action();    
或者:  App::make('request')->action() 
或者:  Facade::make('request')->action()
或者:  Facade::createFacade('request')->action()

此种方法最终调用了容器类Container,所以说容器类是所有方法调用的源头,容器是管理依赖注入的工具,这个是tp6的精华。

第四种:app类非静态调用
 use think\App;
 (new App())->make('request')->action();
第五种:容器类直接调用
Container::getInstance()->make('request')->action();
第六种:超级助手函数app()调用:
app('request')->action();
或使用下面的方式(以下方式即将被弃用)
$request= \think\Request::instance();
$module = $request->module();//模块名
$controller = $request->controller();//控制器名
$action = $request->action()//方法名

另外附PHP获取类中的所有方法。 在当前类中直接使用:

$A = get_class_methods($this); 
halt($A); 

我们打印一下,发现返回的是一个数组,里面包含了当前类的,父类的,父父类的所有方法。 是方法的列表集。
在tp6中使用facade方式来静态调用里面的方法都是可以的,所以facade不仅仅是简单的一个封装,而是可以对以前写的方法全部使用静态调用,这样可以避免实例化,直接使用其中的方法。

发布了124 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_42433970/article/details/102916193