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
12,567 PointsCakePHP: view content from Controller and view on a separate controller and view
I have a
Controller/PostsController.php Model/Post.php View/Posts/Index.ctp if anyone has followed the cakephp blog tutorial i have completed that. Anyone who hasnt, its basically a site that lets you add a title and a comment. Much like a forum.
Now i have this set up , but i want to include a new page called home. And on this page i wont to display the forum i created but in a smaller container.
I am unsure how to add the view of Posts to a new controller/model/view.
This is PostsController.php:
class PostsController extends AppController {
public function index() {
$this->set('posts', $this->Post->find('all'));
}
}
This is View/Posts/index.ctp
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Actions</th>
<th>Created</th>
</tr>
<!-- Here's where we loop through our $posts array, printing out post info -->
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post['Post']['id']; ?></td>
<td>
<?php
echo $this->Html->link(
$post['Post']['title']
);
?>
</td>
<td>
<?php echo $post['Post']['created']; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
so with this, how do i get the same posts to include on a new controller and view, lets say:
HomesController.php View/Homes/index.ctp
1 Answer
thomascawthorn
22,986 PointsHey Matthew,
You could create a 'Pages' Controller and for each page create a method (Home, About, Contact etc...). This is probably better done for static pages where you're simply rendering templates - but it might be easier to manage if your homepage requires very little controller code i.e. you're only grabbing some posts.
Create a new route for '/' or 'index' (whatever cake uses) and send it to the PagesController#Home Method or the HomeController#IndexMethod (in short, a relevant controller#method - it doesn't really matter because the outcome will be the same. It just depends on your brain :p ).
In that method, you're doing exactly the same as you've done above, just rendering a different template.
<?php
class HomeController extends AppController {
public function index() {
$this->set('posts', $this->Post->find('all'));
// then render the correct template and send in the 'posts' variable with the render.
// This is how it's done with laravel - maybe not cake - essentially you're submitting the variable
// containing the selected posts to the view so you can loop or display etc
}
}
?>
It sounds like you're trying to summarise your forum section on the home page - so you may want to find only the most recent 4 posts or popular posts etc. It looks like you could add this condition in the find method.
<?php
$this->set('posts', $this->Post->find('/* Restrict your database query here maybe? */'));
?>
thomascawthorn
22,986 Pointsthomascawthorn
22,986 PointsI would recommend looking into the laravel or Rails4 courses on Treehouse because it sounds like most of your questions are regarding MVC in general as opposed to cake specifically. I'm only able to have a stab at answering your questions because I've been through those courses, so it may help :)