Yii中的评论系统
我试图从这个例子在yii1.1中创建注释,但是在post页面上出现了404错误(应该在页面底部呈现内容和注释表单)。我想我在PostController中的actionView中有错误,如示例代码所示Yii中的评论系统,yii,Yii,我试图从这个例子在yii1.1中创建注释,但是在post页面上出现了404错误(应该在页面底部呈现内容和注释表单)。我想我在PostController中的actionView中有错误,如示例代码所示 $post=$this->loadModel(); $comment=$this->newComment($post); $this->render('view',array( 'model'=>$post, 'comme
$post=$this->loadModel();
$comment=$this->newComment($post);
$this->render('view',array(
'model'=>$post,
'comment'=>$comment,
));
在我的控制器中,初始代码是$this->render('view',array('model')=>$this->loadModel($id),
我在gii中创建了注释模型,并在crud中创建了注释
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();
}
在受保护的/views/post/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>
在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错误,因为它找不到任何要加载的内容。在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错误,因为它找不到任何要加载的内容。在问题的最后一行,您的意思是yii吗?显示调用acction的代码/url。。然后还显示所有操作代码(也包括操作声明函数)添加了代码。我不知道你是如何调用该操作的。但是无论如何,你确定你在views/comment目录中有一个view.php文件吗?我没有像示例中那样的文件夹和视图。他们没有在你问题的最后一行提到应该有这样的视图,你是说yii?显示调用该操作的代码/url。然后显示所有动作代码(动作函数声明)都添加了代码。我不知道如何调用动作。但是无论如何,你确定你在views/comment目录中有view.php文件吗?我没有像示例中那样的文件夹和视图,他们没有提到应该有这样的视图