/** * Finds the Article model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Article the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if ($id !== null && ($model = Article::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('请求页面不存在'); } }
public function actionView($slug) { $model = new Comment(); if ($model->load(Yii::$app->request->post())) { $model->created_at = new Carbon(); $model->updated_at = new Carbon(); $model->user_id = Yii::$app->user->identity->id; if ($model->save()) { $model->article->updateAllCounters(['comments_count' => 1]); return $this->redirect(['view', 'slug' => $slug]); } } $article = Article::findOne(['slug' => $slug]); if (is_null($article)) { throw new NotFoundHttpException('请求页面不存在'); } return $this->render('view', ['article' => $article, 'model' => $model]); }