Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7680025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:00:52+00:00 2026-05-31T18:00:52+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:00:53+00:00Added an answer on May 31, 2026 at 6:00 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.