Home > Backend Development > PHP Tutorial > Yii implements the method of inserting comment forms into the article details page of a single-user blog system

Yii implements the method of inserting comment forms into the article details page of a single-user blog system

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-29 09:08:40
Original
847 people have browsed it

The example in 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:

<&#63;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;

Copy after login

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('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;
}
...

Copy after login

models/Post.php page:

...
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();
}
...

Copy after login

post/view.php page:

...
<div>
<h3>Leave a Comment</h3>
<&#63;php if(Yii::app()->user->hasFlash('commentSubmitted')): ?>
 <div>
 <&#63;php echo Yii::app()->user->getFlash('commentSubmitted'); ?>
 </div>
<&#63;php else: &#63;>
 <&#63;php $this->renderPartial('/comment/_form',array(
 'model'=>$comment,
 )); ?>
<&#63;php endif; &#63;>
</div><!-- comments -->
...

Copy after login

I hope this article will be helpful to everyone’s PHP program design based on the Yii framework.

The above introduces the Yii method of inserting a comment form into the article details page of a single-user blog system, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
How to load the comment list through ajax
From 1970-01-01 08:00:00
0
0
0
User comment question
From 1970-01-01 08:00:00
0
0
0
<!-- -->What is the shortcut for this comment?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template