public function actionView($id) { $model = Post::findOne(['id' => $id, 'status' => 1]); if ($model !== null) { return $this->render('view', ['model' => $model]); } else { throw new NotFoundHttpException('The requested post does not exist.'); } }
public function actionView() { $id = \Yii::$app->request->get('id', 0); $alias = \Yii::$app->request->get('alias', 'non-alias'); Post::updateAllCounters(['views' => 1], ['id' => $id]); $cache = \Yii::$app->getCache(); $cache->keyPrefix = 'post-'; $data = $cache->get('post_' . $id); if ($data === false) { $post = Post::findOne(['id' => $id, 'alias' => $alias]); if ($post) { $data = $post; $cache->set('post_' . $id, $data); } else { throw new NotFoundHttpException('Post not found'); } } //var_dump($data);die(); \Yii::$app->view->title = $data->title; return $this->render('view', ['post' => $data]); }
protected function findModel($id) { if (is_array($id)) { $model = Post::findAll($id); } else { $model = Post::findOne($id); } if ($model !== null) { return $model; } else { throw new HttpException(404); } }
/** * Finds the Post model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * * @param integer $id * @return Post The loaded model. * * @throws NotFoundHttpException if the model cannot be found. */ public function findModel($id) { if (($model = Post::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }