Exemplo n.º 1
0
 public function actionView($id, $slug, $parentSectionId, $parentSectionSlug, $childSectionId, $childSectionSlug)
 {
     /** @var Post $post */
     $post = Post::find()->where(['id' => $id, 'slug' => $slug])->with(['section', 'section.parent'])->one();
     if ($post === null || $post->section->parent->id != $parentSectionId || $post->section->parent->slug != $parentSectionSlug || $post->section->id != $childSectionId || $post->section->slug != $childSectionSlug) {
         throw new HttpException(404, 'Cannot find the requested post.');
     }
     $comment = Comment::create(['post_id' => $post->id]);
     if (Yii::$app->getRequest()->getIsAjax() && $comment->load(Yii::$app->getRequest()->post())) {
         Yii::$app->getResponse()->format = Response::FORMAT_JSON;
         return ActiveForm::validate($comment);
     }
     if ($comment->load(Yii::$app->getRequest()->post()) && $comment->save()) {
         return $this->refresh();
     }
     $comments = Comment::findByPost($post->id);
     return $this->render('view', ['post' => $post, 'comment' => $comment, 'comments' => $comments]);
 }