Home  >  Article  >  Backend Development  >  How to use Yii to insert a comment form into the article details page of a single-user blog system

How to use Yii to insert a comment form into the article details page of a single-user blog system

不言
不言Original
2018-06-19 14:51:281184browse

This article mainly introduces Yii's method of inserting a comment form into the article details page of a single-user blog system. It analyzes the specific skills of Yii to implement the comment form function on the article details page with examples. Friends who need it can refer to it

The example of this article describes Yii's method of inserting a comment form into the article details page of a single-user blog system. Share it with everyone for your reference, the details are as follows:

action part:

<?php
function test($objs)
{
 $objs->var=10;
}
class one
{
 public $var=1;
}
$obj=new one();
echo $obj->var.&#39;<p>&#39;;
test($obj);
echo $obj->var;
exit;

PostController.php page:

...
/**
* 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(&#39;view&#39;,array(
    &#39;model&#39;=>$post,
    &#39;comment&#39;=>$comment,
  ));
}
protected function newComment($post)
{
  $comment=new Comment();
  if(isset($_POST[&#39;Comment&#39;]))
  {
   $comment->attributes=$_POST[&#39;Comment&#39;];
   if($post->addComment($comment))//==============================
   {
    if($comment->status==Comment::STATUS_PENDING)
     Yii::app()->user->setFlash(&#39;commentSubmitted&#39;,&#39;Thank you...&#39;);
    $this->refresh();
   }
  }
  return $comment;
}
...

models/Post.php page:

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

post/view.php page:

...
<p id="comments">
<h3>Leave a Comment</h3>
<?php if(Yii::app()->user->hasFlash(&#39;commentSubmitted&#39;)): ?>
 <p class="flash-success">
 <?php echo Yii::app()->user->getFlash(&#39;commentSubmitted&#39;); ?>
 </p>
<?php else: ?>
 <?php $this->renderPartial(&#39;/comment/_form&#39;,array(
 &#39;model&#39;=>$comment,
 )); ?>
<?php endif; ?>
</p><!-- comments -->
...

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Usage analysis of the front-end resource package that comes with the Yii framework in PHP

About Yii Implementation of methods for handling front and back logins

The above is the detailed content of How to use Yii to insert a comment form into the article details page of a single-user blog system. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn