在CakePHP中向文章添加注释 [英] Adding a comment to an article in CakePHP

查看:99
本文介绍了在CakePHP中向文章添加注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习CakePHP,我的第一个MVC,我有一些最佳实践的问题。

I'm learning CakePHP, my first MVC, and I have a few "best practice" questions.

这是我的观点,显示一个新闻文章: / p>

This is my view for displaying a news article:

<h1><?php echo h($post['Post']['title'])?></h1>
<p><?php echo h($post['Post']['body'])?></p>

<?php foreach ($post['Comment'] as $comment): ?>
<div class="comment" style="margin-left:50px;">
        <p><?php echo h($comment['body'])?></p>
    </div>
<?php endforeach;

echo $this->element('newcomment', array("post_id" => $post['Post']['id']));?>



我不认为你可以使用添加视图在另一个视图中添加注释,所以我创建了一个元素。我希望这是这个的最佳实践。

I didn't think you could use the "add" view for adding a comment in another view, so I created an element. I hope that's best practice for this one.

我的主要问题是:添加评论。
我在表单中添加隐藏字段,或者将其添加到表单的操作中?

My main "problem" was: adding a comment. Do I add a hidden field to the form, or do I add it to the form's action?

我使用id in action因为它更容易重用它为重定向之后。这是newcomment元素:

I went with the "id in action" part, because it's easier to reuse it for a redirect afterwards. This is the newcomment element:

<h1>Add Comment</h1>
<?php
echo $this->Form->create('Comment',array('action' => 'add',
                                             'url' => array($post_id)));
echo $this->Form->input('body', array('rows' => '3'));
echo $this->Form->end('Add comment');
?>

然后这是CommentsController中的add函数:

And then this is the "add" function in the CommentsController:

public function add($post_id = null) {
    if ($this->request->is('post')) {
        $this->Comment->set(array('post_id'=>$post_id));
        if ($this->Comment->save($this->request->data)) {
            $this->Session->setFlash('Your comment has been added.');
            //$this->redirect(array('action' => 'index'));
            $this->redirect(array('controller' => 'posts', 'action' => 'view', $post_id));
        } else {
            $this->Session->setFlash('Unable to add your comment.');
        }
    }
}

我希望在这里提出这些问题。使用最佳做法对我来说非常重要。

I hope it's OK to ask these kinds of questions here. Using the best practice is pretty important to me.

推荐答案

将您的问题作为概念概述,而不是逐行

Looking at your question as an overview of concept, and not line-by-line, there's no problem with this general structure/way of doing it.

我们通常有一个comments元素,其中包含一切 - 评论,新的评论框...等等。然后,如果您不希望用户能够对该特定内容发表评论,您可以传递一个变量,也可以传递一个变量来显示您想要显示的评论数量等。这不意味着它更好 - 只是最适合我们。

We usually have a "comments" element that has everything in it - comments, new comment box...etc. Then, you can pass a variable if you don't want users to be able to comment on that particular thing, or a variable for how many comments you want to show...etc. That doesn't mean it's any better - just works best for us. Each site may present different scenarios which make doing it a different way better.

我试图向许多事情(包括CakePHP)询问最佳实践问题, ,我发现,通常没有直接的答案。如果您的代码简单,干净,组织良好,并处理任何安全/数据完整性问题,您就可以。

I've tried asking the "best practice" question for a lot of things (including CakePHP), and what I've found is, there is usually no straight answer. If your code is simple, clean, well organized, and deals with any security / data-integrity issues, you're fine.

我想的唯一的事情是Ajax评论是多么好。

The only thing I would think about is how nice Ajax comments are. Users are getting spoiled, and having the page refresh just to comment on something might be considered a nuisance.

是否使用隐藏字段或网址完全由您决定 - 只要代码处理数据是固定的,它就不重要了,再次,所有归结为优先。

Whether to use a hidden field or url is completely up to you - as long as the code processing the data is solid, it shouldn't matter at all, and again, all boils down to preference.

这篇关于在CakePHP中向文章添加注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆