6

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.

Moe Far
  • 2,742
  • 2
  • 23
  • 41
user558134
  • 1,071
  • 6
  • 25
  • 38

9 Answers9

13

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;
David Newcomb
  • 10,639
  • 3
  • 49
  • 62
Rajat Singhal
  • 11,234
  • 5
  • 38
  • 56
  • 2
    In my case, the code is: `$controller = Yii::app()->getController(); $isHome = $controller->action->id === $controller->defaultAction ? true : false;` – Meetai.com Dec 28 '13 at 06:40
6

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...

rockstardev
  • 13,479
  • 39
  • 164
  • 296
2

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';
  }
?>
Kalpesh Desai
  • 1,405
  • 15
  • 17
1

if by 'homepage' you mean 'frontpage' then you can check this extension that does exactly this.

Boaz Rymland
  • 1,459
  • 11
  • 29
1

you could check the homepage using the extension pageChecker:

http://www.yiiframework.com/extension/pagechecker
Sharique
  • 21
  • 2
  • Could you explain a bit more about this extension? A link alone is a bit little to answer a question. – Gung Foo Oct 04 '12 at 11:49
1

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

Chanuka Asanka
  • 2,798
  • 3
  • 24
  • 36
1
$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

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
1
if(Url::current() == '/index.php?r=site%2Findex' || Url::current() == Url::home()){
1
namespace common\helpers;

class Url extends \yii\helpers\Url
{
    public static function isHome()
    {
        return (self::home() == Yii::$app->request->url);
    }
}
gvanto
  • 1,936
  • 3
  • 21
  • 26
  • 1
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – double-beep Mar 30 '19 at 19:32