'Adding a comment to an article in CakePHP
I'm learning CakePHP, my first MVC, and I have a few "best practice" questions.
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?
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');
?>
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.');
}
}
}
Is that how it should be?
I hope it's OK to ask these kinds of questions here. Using the best practice is pretty important to me.
Solution 1:[1]
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.
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.
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.
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dave |
