Home > php教程 > php手册 > Yii实现单用户博客系统文章详情页插入评论表单的方法,yii表单

Yii实现单用户博客系统文章详情页插入评论表单的方法,yii表单

WBOY
Release: 2016-06-13 08:48:48
Original
1369 people have browsed it

Yii实现单用户博客系统文章详情页插入评论表单的方法,yii表单

本文实例讲述了Yii实现单用户博客系统文章详情页插入评论表单的方法。分享给大家供大家参考,具体如下:

action部分:

<&#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页面:

...
/**
* 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页面:

...
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页面:

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

Copy after login

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

您可能感兴趣的文章:

  • YiiFramework入门知识点总结(图文教程)
  • Yii入门教程之目录结构、入口文件及路由设置
  • Yii入门教程之Yii安装及hello world
  • Yii PHP Framework实用入门教程(详细介绍)
  • Yii查询生成器(Query Builder)用法实例教程
  • YII使用url组件美化管理的方法
  • Yii中CGridView实现批量删除的方法
  • Yii快速入门经典教程
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template