Yii检查主页 [英] Yii check if homepage

查看:106
本文介绍了Yii检查主页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Yii中是否有内置方法或属性来检查页面是否为主页?

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.

谢谢.

推荐答案

如果要检查当前页面,即action是当前控制器的默认值..

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可能并不总是索引",可以更改,因此您 需要将其与defaultAction进行比较..

dafeultaction may not always be 'index', it can be changed, so you need to compare it with defaultAction instead..

如果您是指站点的默认页面,则按主页,然后还需要将控制器与defaultController进行比较..

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;

在Yii2中:

$controller = Yii::$app->controller;
$default_controller = Yii::$app->defaultRoute;
$isHome = (($controller->id === $default_controller) && ($controller->action->id === $controller->defaultAction)) ? true : false;

这篇关于Yii检查主页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆