/**
* Displays homepage.
*
* @return mixed
*/
public function actionIndex($slug = 'index')
{
// display home page
if (empty($slug) || $slug == 'index') {
$query = Post::find()->where(['status' => Post::STATUS_PUBLISHED]);
$countQuery = clone $query;
$pagination = new Pagination(['totalCount' => $countQuery->count(), 'defaultPageSize' => 5]);
$posts = $query->offset($pagination->offset)->limit($pagination->limit)->all();
return $this->render('index', ['posts' => $posts, 'pagination' => $pagination]);
}
//try to display action from controller
try {
return $this->runAction($slug);
} catch (\yii\base\InvalidRouteException $ex) {
}
//try to display static page from datebase
$page = Page::getDb()->cache(function ($db) use($slug) {
return Page::findOne(['slug' => $slug, 'status' => Page::STATUS_PUBLISHED]);
}, 3600);
if ($page) {
$pageAction = new PageAction($slug, $this, ['slug' => $slug, 'page' => $page]);
return $pageAction->run();
}
//try to display post from datebase
$post = Post::getDb()->cache(function ($db) use($slug) {
return Post::findOne(['slug' => $slug, 'status' => Post::STATUS_PUBLISHED]);
}, 3600);
if ($post) {
$postAction = new PostAction($slug, $this, ['slug' => $slug, 'post' => $post]);
return $postAction->run();
}
//if nothing suitable was found then throw 404 error
throw new \yii\web\NotFoundHttpException('Page not found.');
}