Yii check if homepage(Yii 检查主页)
问题描述
Yii 中是否有内置方法或属性来检查页面是否为主页?
我知道我可以使用这样的东西:
$controller = Yii::app()->getController();$isHome = $controller->getAction()->getId() === 'index' ?真假;
或者把它放在主控制器的一个方法中,但我正在寻找更干净的东西.
谢谢.
如果要查看当前页面,即 action 是当前控制器的默认..
$controller = Yii::app()->getController();$isHome = $controller->action->id === $controller->defaultAction->id ?真假;
<块引用>
dafeultaction 可能并不总是'index',它可以改变,所以你需要将它与 defaultAction 进行比较..
如果你的意思是网站的默认页面,那么你需要将你的控制器与 defaultController
.. 进行比较.
$controller = Yii::app()->getController();$default_controller = Yii::app()->defaultController;$isHome = (($controller->id === $default_controller->id) && ($controller->action->id === $controller->defaultAction->id)) ?真假;
在 Yii2 中:
$controller = Yii::$app->controller;$default_controller = Yii::$app->defaultRoute;$isHome = (($controller->id === $default_controller) && ($controller->action->id === $controller->defaultAction)) ?真假;
Is there a buildin method or property in Yii to check if page is homepage?
I know i can use something like this:
$controller = Yii::app()->getController();
$isHome = $controller->getAction()->getId() === 'index' ? true : false;
Or put it in a method in main controller, but i am looking for something cleaner.
Thanks.
If You want to check the current page, ie action is the default of the current controller..
$controller = Yii::app()->getController();
$isHome = $controller->action->id === $controller->defaultAction->id ? true : false;
dafeultaction may not always be 'index', it can be changed, so you need to compare it with defaultAction instead..
And by homepage if you mean the defult page of site, then you need to compare your controller also with defaultController
..
$controller = Yii::app()->getController();
$default_controller = Yii::app()->defaultController;
$isHome = (($controller->id === $default_controller->id) && ($controller->action->id === $controller->defaultAction->id)) ? true : false;
In Yii2:
$controller = Yii::$app->controller;
$default_controller = Yii::$app->defaultRoute;
$isHome = (($controller->id === $default_controller) && ($controller->action->id === $controller->defaultAction)) ? true : false;
这篇关于Yii 检查主页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Yii 检查主页


基础教程推荐
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01