TP6獲取控制器名,方法名

2021-09-29 08:33:32 字數 1743 閱讀 5878

以下呼叫方法自選一種,哪種好玩你就用哪種。

第一種:助手函式呼叫

request()

->

controller()

//獲取控制器名

request()

->

action()

//獲取方法名

第二種:門面函式facade的靜態呼叫
\think\facade\request::

action()

\think\facade\request::

controller

()

use

;use

think\facade;:

class):

:make

('request')-

>

action()

;

:make

('request')-

>

action()

或者: facade:

:make

('request')-

>

action()

或者: facade:

:createfacade

('request')-

>

action

()

此種方法最終呼叫了容器類container,所以說容器類是所有方法呼叫的源頭,容器是管理依賴注入的工具,這個是tp6的精華。

use;(

new())

->

make

('request')-

>

action()

;

第五種:容器類直接呼叫
container:

:getinstance()

->

make

('request')-

>

action()

;

(

'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不僅僅是簡單的乙個封裝,而是可以對以前寫的方法全部使用靜態呼叫,這樣可以避免例項化,直接使用其中的方法。

Yii2 獲取模組名 控制器名 方法名

yii2 獲取模組名 控制器名 方法名 在檢視中 模組名 this context module id 控制器名 this context id 方法名 this context action id在控制器中 或模組名 this module id 控制器名 this id 方法名 this act...

Yii2 獲取模組名 控制器名 方法名

yii2 獲取模組名 控制器名 方法名 在檢視中 模組名 this context module id 控制器名 this context id 方法名 this context action id 在控制器中 或模組名 this module id 控制器名 this id 方法名 this ac...

Yii2 獲取模組名 控制器名 方法名

yii2 獲取模組名 控制器名 方法名 在檢視中 模組名 this context module id 控制器名 this context id 方法名 this context action id 在控制器中 模組名 yii app co ntro ller mo dule id 控制器 名yii...