title: Adding a comment to an article in CakePHP
body: <p>I'm learning CakePHP, my first MVC, and I have a few "best practice" questions.</p>
  
  <p>This is my view for displaying a news article:</p>
  
  <pre><code>&lt;h1&gt;&lt;?php echo h($post['Post']['title'])?&gt;&lt;/h1&gt;
  &lt;p&gt;&lt;?php echo h($post['Post']['body'])?&gt;&lt;/p&gt;
  
  &lt;?php foreach ($post['Comment'] as $comment): ?&gt;
  &lt;div class="comment" style="margin-left:50px;"&gt;
          &lt;p&gt;&lt;?php echo h($comment['body'])?&gt;&lt;/p&gt;
      &lt;/div&gt;
  &lt;?php endforeach;
  
  echo $this-&gt;element('newcomment', array("post_id" =&gt; $post['Post']['id']));?&gt;
  </code></pre>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p>I went with the "id in action" part, because it's easier to reuse it for a redirect afterwards. This is the newcomment element:</p>
  
  <pre><code>&lt;h1&gt;Add Comment&lt;/h1&gt;
  &lt;?php
  echo $this-&gt;Form-&gt;create('Comment',array('action' =&gt; 'add',
                                               'url' =&gt; array($post_id)));
  echo $this-&gt;Form-&gt;input('body', array('rows' =&gt; '3'));
  echo $this-&gt;Form-&gt;end('Add comment');
  ?&gt;
  </code></pre>
  
  <p>And then this is the "add" function in the CommentsController:</p>
  
  <pre><code>public function add($post_id = null) {
      if ($this-&gt;request-&gt;is('post')) {
          $this-&gt;Comment-&gt;set(array('post_id'=&gt;$post_id));
          if ($this-&gt;Comment-&gt;save($this-&gt;request-&gt;data)) {
              $this-&gt;Session-&gt;setFlash('Your comment has been added.');
              //$this-&gt;redirect(array('action' =&gt; 'index'));
              $this-&gt;redirect(array('controller' =&gt; 'posts', 'action' =&gt; 'view', $post_id));
          } else {
              $this-&gt;Session-&gt;setFlash('Unable to add your comment.');
          }
      }
  }
  </code></pre>
  
  <p>Is that how it should be?</p>
  
  <p>I hope it's OK to ask these kinds of questions here. Using the best practice is pretty important to me.</p>