I'm trying to come up to speed on CakePHP. I have used the MVC pattern before and am familiar with the idea. I have tried to follow the Blog tutorial for 2.* version of CakePHP and am having no luck.
If I navigate to http://localhost/posts/index
, I see this:
Not Found
The requested URL /Posts was not found on this server.
It all looks fine if I just load http://localhost/
The other thing I don't get is how the Controller is calling:
$this->Post->find(’all’));
There is no method called find
on the Post model. The model is completely bare:
class Post extends AppModel {
}
I don't know what to make of that. Does the framework generate a find method or has the write of the tutorial omitted a very important part of it?
Edit - more details There is a controller in the folder app/Controller called PostsController:
class PostsController extends AppController {
public $helpers = array(’Html’, ’Form’);
public function index() {
$this->set(’posts’, $this->Post->find(’all’));
}
public function view($id = null) {
if (!$id) {
throw new NotFoundException(__(’Invalid post’));
}
$post = $this->Post->findById($id);
if (!$post) {
throw new NotFoundException(__(’Invalid post’));
}
$this->set(’post’, $post);
}
}
There is an index view inside /app/View/Posts/
<!-- File: /app/View/Posts/index.ctp -->
<h1>Blog posts</h1>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Created</th>
</tr>
<!-- Here is 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(’controller’ => ’posts’, ’action’ => ’view’, $post[’Post’][’id’])); ?>
</td>
<td><?php echo $post[’Post’][’created’]; ?></td>
</tr>
<?php endforeach; ?>
<?php unset($post); ?>
</table>
The model is as set out in the original post above.
In the database, there is the following data which I used in the tutorial:
/* First, create our posts table: */
CREATE TABLE posts (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(50),
body TEXT,
created DATETIME DEFAULT NULL,
modified DATETIME DEFAULT NULL
);
/* Then insert some posts for testing: */
INSERT INTO posts (title,body,created)
VALUES (’The title’, ’This is the post body.’, NOW());
INSERT INTO posts (title,body,created)
VALUES (’A title once again’, ’And the post body follows.’, NOW());
INSERT INTO posts (title,body,created)
VALUES (’Title strikes back’, ’This is really exciting! Not.’, NOW());