Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Matthew Smart
Matthew Smart
12,567 Points

How to include separate Controller,Model,View with CakePHP

New to Cakephp and im struggling a little bit, wonder if you could help...

so ive followed CakePHP blog tutorial and completed it. after making the blog, the controller is called PostsController.php the model is Post.php and the view is in Posts/index.ctp.

I will provide code(not the full as only the relevent needed to explain)

Controller/PostsController.php is as follows:

class PostsController extends AppController {
    public $helpers = array('Html', 'Form');

    public function index() {
         $this->set('posts', $this->Post->find('all'));
         $this->set('title_for_layout', 'forums');

   public function add() {
        if ($this->request->is('post')) {
            if ($this->Post->save($this->request->data)) {
                $this->Session->setFlash(__('Your post has been saved.'));
                return $this->redirect(array('action' => 'index'));
            $this->Session->setFlash(__('Unable to add your post.'));


View/Posts/index.ctp is as follows:

<?php echo "<h1>" . $this->Html->link('Add Post', array('action' => 'add')); ?>

<table style="float: left; margin-left: 100px; display: none; " id="hidethis">

<!-- Here's where we loop through our $posts array, printing out post info -->

    <?php foreach ($posts as $post): ?>
        <td width="22%" style=" font-size: .75em; text-align: left;">
            <?php echo $post['Post']['created']; ?>

        <td width="78%">


    <?php endforeach; ?>


Model/Post.php is as follows:

class Post extends AppModel {
     public $validate = array(
        'title' => array(
            'rule' => 'notEmpty'
        'body' => array(
            'rule' => 'notEmpty'

so this works fine,however i want to have a website with 3 main pages; Home, About us, Contact Us.

now on the about us page i will have content and a section i want to be the posts that i created as seen in the examples above. However to do this i want to be able to have: Controller/AboutsController.php, Model/About.php view/Abouts/about.ctp and kind of require/include the whole Posts MVC.

and if i was to just make my about us page the Posts MVC then the names wouldnt be really what i wont them to, meaning the pages would be called posts instead of about, due to cakephp naming conventions.

I hope this makes sense, can any one help please?

1 Answer


I haven't used Cake specifically but maybe I can offer some advice regarding MVC.

It sounds like you have a really great Posts setup already, and you want your posts to appear in multiple places across the site. Don't change a single thing here - you're all set up.

If it as me, I would create only an About Controller and an About View. You will need a method in the About Controller for index/show. In this method you will need to grab the posts you want to display from the Posts Model (maybe all or most recent etc). You'll then need to render a view and also send in the posts to the view. Again - not too sure about Cake - but models don't have to be specific to their controllers. You can call models (which is, at it's most basic, a row in your database with logic) from any controller as well as relationships (one to many, belongs to).

If Cake has routes, you'll need to route any requests for '/about' to the about#index method in the about controller.

Inside the about view, you'll need to grab and foreach around whatever variable you send from the About Controller and there you have it!

If you're displaying all posts via the posts/index, you could change that to the about page. All you would need to do is route '/about' to the posts#index method and render a view which has the about-page-information along with code that will display your posts. This seems a little conflicted, which is why it may be simpler work flow to create an About Controller.

Hope this helps!

Matthew Smart
Matthew Smart
12,567 Points

Thanks a lot for trying to answer, even though you dont know Cakephp, im sure all MVC frameworks work in the same kind of way. Its just im finding it hard to learn cakephp as it only has a manual and there or minimal to non-existing tutorials for this framework. But i can see the potential in it and will continue learning.

Thanks for your answer, i will give this a try and see if i can get it to work :)

No worries! Can I ask why you've gone down the CakePHP route? If you haven't already spotted it, Treehouse have recently released an initial guide to Laravel. Laravel is on of the most popular php MVC frameworks knocking around. It has good tutorials on youtube/blogs and a (fairly inexpensive) dedicated screencast service called laracasts.

Matthew Smart
Matthew Smart
12,567 Points

Purely, because i work in 1/2 line Support at an insurance company. i want to move into web development and there is a 50/50 chance a new developer is going to arise. The websites that are here left behind by the old web developer are all written with CakePHP. So in the hope this job becomes available and i have a good chance of getting it if it does, i wanted to get familiar with the framework.

Thing is im not sure if cakephp will benefit me if i was to look for jobs else where. What do you think?

I'm not really in a position to give advice but I do know from my own job hunting / applications that experience with the current popular php frameworks can be a desirable trait. That said, although every framework is different, once you've got your head around the general concept of MVC and associated best practices, I'm sure you would find it quite easy to pick up others! Best of luck with this job

Matthew Smart
Matthew Smart
12,567 Points

Tom, could you please look at my question in the forums please if you have chance? followed what you have said but around 70% there. its under PHP :)