Reputation: 107
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
EDIT//EDIT//EDIT
right i have managed to do half of this now by adding $this->render('/Abouts/index'); to my PostsController
but now i get an error saying "Undefined variable: posts [APP\View\Abouts\index.ctp, line 12]"
im not sure how to define these in my abouts view
Upvotes: 0
Views: 77
Reputation: 5767
From cakephp books
Many applications have small blocks of presentation code that need to be repeated from page to page, sometimes in different places in the layout. CakePHP can help you repeat parts of your website that need to be reused. These reusable parts are called Elements. Ads, help boxes, navigational controls, extra menus, login forms, and callouts are often implemented in CakePHP as elements. An element is basically a mini-view that can be included in other views, in layouts, and even within other elements. Elements can be used to make a view more readable, placing the rendering of repeating elements in its own file. They can also help you re-use content fragments in your application.
So, for your purposes use elements, more information here
Upvotes: 0
Reputation: 29141
In the 'abouts' action, you'd do the same thing you were doing in the index:
$this->set('posts', $this->Post->find('all'));
Site note: Seems like it would be worth your time to go through the blog tutorial again for a refresher. I believe this kind of thing is covered pretty thoroughly.
Upvotes: 1