Пример #1
0
 /**
  * Displays a single Post model.
  * @param integer $id
  * @return mixed
  */
 public function actionView($id)
 {
     $id = intval($id);
     $commentmodel = new Comment();
     if ($commentmodel->load(Yii::$app->request->post())) {
         if (Yii::$app->user->isGuest) {
             //登录后才能评论
             Yii::$app->user->returnUrl = Yii::$app->request->getUrl();
             //记录登录后要返回的地址
             return $this->redirect(['/site/login']);
         } else {
             $commentmodel->post_id = $id;
             $result = $commentmodel->dealContent(Yii::$app->request->post());
             if ($result) {
                 $commentmodel->save();
                 $this->refresh();
             } else {
                 Yii::$app->getSession()->setFlash('error', '评论内容过多或盖楼太多,请调整后重新提交评论');
             }
         }
     }
     return $this->render('view', ['model' => $this->findModel($id), 'commentmodel' => $commentmodel]);
 }