本文实例讲述了yii实现单用户博客系统文章详情页插入评论表单的方法。分享给大家供大家参考,具体如下:
action部分:
<?php
function test($objs)
{
$objs->var=10;
}
class one
{
public $var=1;
}
$obj=new one();
echo $obj->var.'<p>';
test($obj);
echo $obj->var;
exit;
postcontroller.php页面:
...
/**
* displays a particular model.
* @param integer $id the id of the model to be displayed
*/
public function actionview($id)
{
$post=$this->loadmodel($id);
$comment=$this->newcomment($post);
$this->render('view',array(
'model'=>$post,
'comment'=>$comment,
));
}
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...');
$this->refresh();
}
}
return $comment;
}
...
models/post.php页面:
...
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();
}
...
post/view.php页面:
...
<div id="comments">
<h3>leave a comment</h3>
<?php if(yii::app()->user->hasflash('commentsubmitted')): ?>
<div class="flash-success">
<?php echo yii::app()->user->getflash('commentsubmitted'); ?>
</div>
<?php else: ?>
<?php $this->renderpartial('/comment/_form',array(
'model'=>$comment,
)); ?>
<?php endif; ?>
</div><!-- comments -->
...
希望本文所述对大家基于yii框架的php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!