简体   繁体   中英

Yii check if homepage

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;

This is what I use to check if I'm on the frontpage:

$isFrontpage = false;
if ((Yii::app()->controller->getId().'/'.Yii::app()->controller->getAction()->getId()) == 'site/index'  ) { 
    $isFrontpage = true;
}

Works like a charm.... even on views...

May be this help you:)

<?php
  $controllerl = Yii::$app->controller;
  $homecheker = $controllerl->id.'/'.$controllerl->action->id;
  if($homecheker=='site/index')
  {
     //no border on home page
     $mymaincls ='main-nav navbar-fixed-top';
  }else
  {
     //border all other page
     $mymaincls ='main-nav navbar-fixed-top header-border';
  }
?>

如果通过'主页'表示'首页',那么您可以检查此扩展程序是否正确执行此操作。

你可以使用扩展页面检查主页查看:

http://www.yiiframework.com/extension/pagechecker

you can compare the current controller and action with the default controller and action.

$controller = Yii::app()->getController();

$default_controller = Yii::app()->defaultController;

$isHome = $controller->getId() === $default_controller && $controller->getAction()->getId() === 'index';

i couldn't access default action via Yii::app() like Yii::app()->defaultController . however you use string to compare.

cheers

$check_home=$path=='site/index.html'?'TRUE':'False';

$path=Yii::$app->request->pathInfo;

do as per your logic if check_home is true or false

i am removing my sidebars on home page

if(Url::current() == '/index.php?r=site%2Findex' || Url::current() == Url::home()){
namespace common\helpers;

class Url extends \yii\helpers\Url
{
    public static function isHome()
    {
        return (self::home() == Yii::$app->request->url);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM