Yii博客系统评论功能概述
本示例详细说明了如何在Yii框架开发的单用户博客系统中,于文章详情页面集成评论输入表单的具体实现方案。该方案可供开发者参考以完善博客系统的交互功能。
首先关注控制器部分的代码逻辑:
<?php
function test($objs)
{
$objs->var=10;
}
class one
{
public $var=1;
}
$obj=new one();
echo $obj->var.'';
test($obj);
echo $obj->var;
exit;
</code></pre>
在PostController.php文件中:
<pre class="brush:php;"><code class="language-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;
}
...
</code></pre>
在models/Post.php文件中:
<pre class="brush:php;"><code class="language-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();
}
...
</code></pre>
在post/view.php视图中:
<pre class="brush:php;"><code class="language-php">
...
<h3>Leave a Comment</h3>
<?php if(Yii::app()->user->hasFlash('commentSubmitted')): ?>
<?php echo Yii::app()->user->getFlash('commentSubmitted'); ?>
<?php else: ?>
<?php $this->renderPartial('/comment/_form',array(
'model'=>$comment,
)); ?>
<?php endif; ?>
...
上述内容旨在为使用Yii框架进行PHP程序开发的读者提供有益的技术参考。