Yii 检查主页

Yii check if homepage(Yii 检查主页)
本文介绍了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 检查主页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)