This article mainly introduces how Yii inserts comment forms into the details page of a single-user blog system article, and analyzes the specific skills of Yii to implement the comment form function on the details page of the article, for more information about Yii, see the following example. We will share this with you for your reference. The details are as follows:
Action:
<?phpfunction test($objs){ $objs->var=10;}class one{ public $var=1;}$obj=new one();echo $obj->var.'';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('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 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();}...
Post/view. php page:
...Leave a Comment<?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; ?>
...
I hope this article will help you design PHP programs based on the Yii Framework.