0
votes

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());
2
set debug level higher than 0 from Config/core.php and then see what localhost/posts/index displays - Kishor Kundan
@KishorKundan The debug level was already at 2. It displays Not Found The requested URL /Posts was not found on this server. - onefootswill

2 Answers

2
votes

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?

Yea, Framework takes care of the ORM part.. I guess you are 'super' new to this.. even I am new to cakephp... I am just 5 projects old in CakePHP, so even I am new...

Ok...

Back to your question:

You need to have a 'Post' Controller and an 'index' action.

Make sure you 'uses' Model else, you can also call it from an action like so:

$this->loadModel('Post');

$this->set($variable, $this->Post->find('all'));

and then in your views

do a :

<?php pr($variable) ?>

What is need is not the 'short term' fish but, the ability to fish yourself... The example I gave above will give you understanding of the CakePHP working.

Questions? :)

EDIT : You have issue with mod-rewrite, that's all!

Do this:

Open app/Config/core.php

find the line and uncomment it:

Configure::write('App.baseUrl', env('SCRIPT_NAME'));

Delete all, .htaccess from all document root dir, app dif, webroot dir...

Solved?

1
votes

The first issue sounds like a problem with mod_rewrite, please check the URL rewriting chapter from the cookbook.

Does the framework generate a find method, or has the write of the tutorial omitted a very important part of it?

No and no. It's plain PHP functionality, you just have to follow the inheritance hierarchy to find where the find method comes from: Post extends AppModel extends Model. And if you check the API you will see that Model defines the find method your Post model inherits.