Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 865
user558134 Avatar asked Sep 09 '12 18:09

user558134


People also ask

Is Yii Framework good?

Advantages of using YiiIt's ideal for rapid development as it has code generators for CRUD operations. Just like Laravel, Yii utilises the DRY coding rule, which is very helpful for writing a well-structured and sophisticated codebase. Yii has many built-in widgets to validate or output data quickly with Ajax support.

What is Yii used for?

Yii is a high-performance, component-based PHP framework for developing large-scale Web applications rapidly. It enables maximum reusability in Web programming and can significantly accelerate your Web application development process.

What is yii1?

Yii is a fast, secure, and efficient PHP framework. Flexible yet pragmatic. Works right out of the box.


1 Answers

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;
like image 109
Rajat Singhal Avatar answered Oct 11 '22 21:10

Rajat Singhal