yii2获取当前控制器(controller)名 、动作名(action)

在控制器的beforeAction方法中:

//eg: frontend\controllers\ArticleController中:
class ArticleController extends Controller
{
    public function beforeAction($action)
    {
        echo $action->controller->module->id;  //app-frontend
        echo $action->controller->id;          //article
        echo $action->id;                      //index
    }
}

在控制器中:

//eg: frontend\controllers\ArticleController中:
class ArticleController extends Controller
{
    public function actionIndex()
    {
        echo $this->module->id;  //app-frontend
        echo $this->id;          //article
        echo $this->action->id;  //index
    }
}

在视图(frontend\controllers\ArticleControllerindex方法对应的视图)中:

echo $this->context->module->id;  //app-frontend
echo $this->context->id;          //article
echo $this->context->action->id;  //index

还有一种在以上三种情况下都通用的方法:

echo Yii::$app->controller->module->id;  //app-frontend
echo Yii::$app->controller->id;          //article
echo Yii::$app->controller->action->id;  //index