Yii check if homepage
Asked Answered
H

9

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.

Heinrik answered 9/9, 2012 at 18:35 Comment(0)
P
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;
Pung answered 10/9, 2012 at 4:12 Comment(1)
In my case, the code is: $controller = Yii::app()->getController(); $isHome = $controller->action->id === $controller->defaultAction ? true : false;Oca
S
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...

Scolecite answered 9/12, 2012 at 5:28 Comment(0)
A
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';
  }
?>
Annmarie answered 20/5, 2016 at 7:41 Comment(0)
B
1

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

Brigid answered 9/9, 2012 at 20:31 Comment(0)
O
1

you could check the homepage using the extension pageChecker:

http://www.yiiframework.com/extension/pagechecker
Ozoniferous answered 3/10, 2012 at 19:11 Comment(1)
Could you explain a bit more about this extension? A link alone is a bit little to answer a question.Midgard
D
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

Drafty answered 17/11, 2013 at 18:31 Comment(0)
H
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

Histopathology answered 19/1, 2015 at 6:37 Comment(1)
//in YII2 //this will work //this will check if home page or notHistopathology
I
1
if(Url::current() == '/index.php?r=site%2Findex' || Url::current() == Url::home()){
Irena answered 30/4, 2015 at 16:15 Comment(0)
T
1
namespace common\helpers;

class Url extends \yii\helpers\Url
{
    public static function isHome()
    {
        return (self::home() == Yii::$app->request->url);
    }
}
Towle answered 30/3, 2019 at 19:2 Comment(1)
While this code may solve the question, including an explanation 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.Grovel

© 2022 - 2024 — McMap. All rights reserved.