Yii check if there is a main page

Is there a built-in method or property in Yii to check if the page is on the main page?

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 the main controller, but I'm looking for something cleaner.

Thank.

+5
source share
8 answers

If you want to check the current page, that is, the default action is the current controller.

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

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

And on the home page, if you mean a page with a website defect, you also need to compare your controller 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;
+10
source

, , , :

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

.... ...

+6

, :)

<?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';
  }
?>
+2

"homepage" "frontpage", , .

+1

:

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

.

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

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

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

Yii:: app(), Yii:: app() → defaultController. .

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

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

, check_home false

+1
if(Url::current() == '/index.php?r=site%2Findex' || Url::current() == Url::home()){
+1
source

All Articles