public function actionCatalog() { if (Yii::$app->request->get('surname')) { $cacheId = Yii::$app->params["domain"] . '_blog_catalog_' . md5(serialize(['surname' => Yii::$app->request->get('surname')])); $catalog = Yii::$app->cache->get($cacheId); if ($catalog === false) { $catalog = BlogCatalog::findOne(['surname' => Yii::$app->request->get('surname')]); Yii::$app->cache->set($cacheId, $catalog, $this->cacheDuration); } if ($catalog) { $query = BlogPost::find(); $query->where(['status' => Status::STATUS_ACTIVE, 'catalog_id' => $catalog->id]); } else { $this->redirect(['blog/index']); } } else { $this->redirect(['blog/index']); } if (Yii::$app->request->get('tag')) { $query->andFilterWhere(['tags' => Yii::$app->request->get('tag')]); } if (Yii::$app->request->get('keyword')) { //$keyword = '%'.strtr(Yii::$app->request->get('keyword'), array('%'=>'\%', '_'=>'\_', '\\'=>'\\\\')).'%'; $keyword = Yii::$app->request->get('keyword'); $query->andFilterWhere(['title' => $keyword]); } $paginationParam = ['defaultPageSize' => Yii::$app->params['blogPostPageCount'], 'totalCount' => $query->count()]; $cacheId = Yii::$app->params["domain"] . '_blog_post_pagination_' . md5(serialize($paginationParam)); $pagination = Yii::$app->cache->get($cacheId); if ($pagination === false) { $pagination = new Pagination($paginationParam); Yii::$app->cache->set($cacheId, $pagination, $this->cacheDuration); } $query = $query->orderBy('created_at desc')->offset($pagination->offset)->limit($pagination->limit); $cacheId = Yii::$app->params["domain"] . '_blog_post_' . md5(serialize($query)); $posts = Yii::$app->cache->get($cacheId); if ($posts === false) { $posts = $query->all(); Yii::$app->cache->set($cacheId, $posts, $this->cacheDuration); } return $this->render('index', ['posts' => $posts, 'pagination' => $pagination]); }
/** * Finds the BlogCatalog model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return BlogCatalog the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = BlogCatalog::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }