2016-09-13 293 views
1

我试图在这个例子中创建Yii 1.1中的评论http://yiiframework.ru/doc/blog/en/comment.create,但是我在帖子页面(应该在页面底部呈现内容和评论表单)上有404错误。我想我已经在PostController中的actionView错误如示例代码Yii评论系统

$post=$this->loadModel(); 
    $comment=$this->newComment($post); 

    $this->render('view',array(
     'model'=>$post, 
     'comment'=>$comment, 
    )); 

,并在我的控制器INTIAL代码为$this->render('view',array('model'=>$this->loadModel($id),

我创建注释模型在GII开创了污物评论。

public function actionView($id) 
    { 
      $post=$this->loadModel(); 
      $comment=$this->newComment($post); 

      $this->render('view',array(
      'model'=>$post, 
      'comment'=>$comment, 
      //$this->loadModel($id),     

     )); 
    } 

protected function newComment($post) 
{ 
    $comment=new Comment; 
    if(isset($_POST['Comment'])) 
    { 
     $comment->attributes=$_POST['Comment']; 
     if($post->addComment($comment)) 
     { 
      if($comment->status==Comment::STATUS_PENDING) 
       Yii::app()->user->setFlash('commentSubmitted','Thank you for your comment. Your comment will be posted once it is approved.'); 
      $this->refresh(); 
     } 
    } 
    return $comment; 
} 

在Post模型:

public function addComment($comment) 
    { 
     if(Yii::app()->params['commentNeedApproval']) 
      $comment->status=Comment::STATUS_PENDING; 
     else 
      $comment->status=Comment::STATUS_APPROVED; 
     $comment->post_id=$this->id; 
     return $comment->save(); 
    } 

在保护/视图/后/ view.php

<div id="comments"> 
    <?php if($model->commentCount>=1): ?> 
     <h3> 
      <?php echo $model->commentCount . 'comment(s)'; ?> 
     </h3> 

     <?php $this->renderPartial('_comments',array(
      'post'=>$model, 
      'comments'=>$model->comments, 
     )); ?> 
    <?php endif; ?> 
</div> 
+0

在你问题的最后一行,你的意思是yii? – ShellZero

+0

显示调用acction的代码/网址。然后显示所有的行动代码(声明行动功能太) – scaisEdge

+0

添加了代码。 – rick1

回答

0

在你的actionView方法,你是不是过的$ id传递给loadModel()。它应该是这样的。

public function actionView($id) 
    { 
      $post=$this->loadModel($id); 
      $comment=$this->newComment($post); 

      $this->render('view',array(
      'model'=>$post, 
      'comment'=>$comment, 
      //$this->loadModel($id),     

     )); 
    } 

否则,loadModel方法将抛出一个404错误,因为它找不到任何要载入的东西。