Yii实现单用户博客系统文章详情页插入评论表单的方法
本文实例讲述了Yii实现单用户博客系统文章详情页插入评论表单的方法。分享给大家供大家参考,具体如下:
action部分:
<?php
functiontest($objs)
{
$objs->var=10;
}
classone
{
public$var=1;
}
$obj=newone();
echo$obj->var.'<p>';
test($obj);
echo$obj->var;
exit;
PostController.php页面:
...
/**
*Displaysaparticularmodel.
*@paraminteger$idtheIDofthemodeltobedisplayed
*/
publicfunctionactionView($id)
{
$post=$this->loadModel($id);
$comment=$this->newComment($post);
$this->render('view',array(
'model'=>$post,
'comment'=>$comment,
));
}
protectedfunctionnewComment($post)
{
$comment=newComment();
if(isset($_POST['Comment']))
{
$comment->attributes=$_POST['Comment'];
if($post->addComment($comment))//==============================
{
if($comment->status==Comment::STATUS_PENDING)
Yii::app()->user->setFlash('commentSubmitted','Thankyou...');
$this->refresh();
}
}
return$comment;
}
...
models/Post.php页面:
...
publicfunctionaddComment($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();
}
...
post/view.php页面:
...
<divid="comments">
<h3>LeaveaComment</h3>
<?phpif(Yii::app()->user->hasFlash('commentSubmitted')):?>
<divclass="flash-success">
<?phpechoYii::app()->user->getFlash('commentSubmitted');?>
</div>
<?phpelse:?>
<?php$this->renderPartial('/comment/_form',array(
'model'=>$comment,
));?>
<?phpendif;?>
</div><!--comments-->
...
希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。