Atualize o
app/View/Posts/index.ctp
Para isso:
<h1>Blog posts</h1>
<p><?php echo $this->Html->link("Add Post", array('action' => 'add')); ?></p>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Action</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'], array('action' => 'view', $post['Post']['id'])); ?>
</td>
<td>
<?php echo $this->Html->link('Edit', array('action' => 'edit', $post['Post']['id'])); ?>
</td>
<td>
<?php echo $post['Post']['created']; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
Deletando Posts
Adicione o 'action' abaixo, ao final do
PostsController.php:
public function delete($id) {
if ($this->request->is('get')) {
throw new MethodNotAllowedException();
}
if ($this->Post->delete($id)) {
$this->Session->setFlash('The post with id: ' . $id . ' has been deleted.');
$this->redirect(array('action' => 'index'));
}
}
Atualize o
app/View/Posts/index.ctp, para:
<h1>Blog posts</h1>
<p><?php echo $this->Html->link('Add Post', array('action' => 'add')); ?></p>
<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'], array('action' => 'view', $post['Post']['id'])); ?>
<td>
<td>
<?php echo $this->Form->postLink(
'Delete',
array('action' => 'delete', $post['Post']['id']),
array('confirm' => 'Are you sure?'));
?>
<?php echo $this->Html->link('Edit', array('action' => 'edit', $post['Post']['id'])); ?>
<td>
<td>
<?php echo $post['Post']['created']; ?>
<td>
</tr>
<?php endforeach; ?>
</table>
Rotas
Com isso, queremos mudar o comportamento padrão do Cake para abrir inicialmente nosso controller PostsController.
Edite o arquivo:
app/Config/routes.php
Remova as linhas como esta:
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
E adicione esta:
Router::connect('/', array('controller' => 'posts', 'action' => 'index'));
Agora, ao abrir o site, ele não mostrará o conteúdo de boas-vindas, mas o nosso blog.
Mais informações sobre Rotas:
Prontinho, temos um pequeno aplicativo funcionando e criado com o Framework CakePHP 2.2.1.
Link para os fontes: