php 在 Yii 中获取当前控制器 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9582237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:11:04  来源:igfitidea点击:

Getting Current Controller ID in Yii

phptemplatesyiiyii-components

提问by Netorica

I want to get the current controller name that handles the current action. but the in my case I will look for the current controller in my main.php in my layout files.

我想获取处理当前操作的当前控制器名称。但就我而言,我将在布局文件的 main.php 中查找当前控制器

this is my small view of my directory structure to give you an idea where is my layout files and the file where i will put my codes in searching of my controller name

这是我的目录结构的小视图,让您了解我的布局文件在哪里以及我将在其中放置代码以搜索控制器名称的文件

  • /protected
  • /themes
    • /mylayout
      • /layouts
        • main.php
        • column1.php
        • column2.php
      • /site
        • index.php
  • /受保护
  • /主题
    • /我的布局
      • /布局
        • 主文件
        • 列1.php
        • 列2.php
      • /地点
        • 索引.php

Is this possible? im trying the following codes but i failed to get my current controller name...

这可能吗?我正在尝试以下代码,但我没有得到我当前的控制器名称...

echo Yii::app()->controller->getId;
echo Yii:app()->getController->id;
echo Yii:app()->controller->uniqueID;

thanks

谢谢

采纳答案by 22francis

<?php echo $this->getUniqueId();?>

<?php echo $this->getUniqueId();?>

this will show current controller

这将显示当前控制器

回答by schmunk

回答by Onkar Janwa

Controller Id :

控制器 ID :

$this->id

Here $this refers to controller.

这里 $this 指的是控制器。

And For getting action id :

并获取操作 id :

$this->action->id

回答by Raz0rwire

You're not actually required to use the static function. Whenever in a view( or template) you can use echo $this->getUniqueId();to get the unique controller ID.

您实际上不需要使用静态函数。无论何时在视图(或模板)中,您都可以使用它echo $this->getUniqueId();来获取唯一的控制器 ID。

回答by TheStoryCoder

Yii2:

Yii2:

Yii::$app->controller->id

Yii::$app->controller->id

(Documentation: Applicationand Controller)

(文档:应用程序控制器