私はCakePHPを理解しようとしています。私は以前にMVCパターンを使用したことがあり、そのアイデアに精通しています。CakePHPの2.*バージョンのブログチュートリアルに従おうとしましたが、うまくいきません。
に移動するとhttp://localhost/posts/index
、次のように表示されます。
見つかりません
要求されたURL/Postsがこのサーバーで見つかりませんでした。
ロードするだけですべて問題ないように見えますhttp://localhost/
私が得られない他のことは、コントローラーがどのように呼び出しているかです:
$this->Post->find(’all’));
find
Postモデルで呼び出されるメソッドはありません。モデルは完全に裸です:
class Post extends AppModel {
}
どうしたらいいのかわからない。フレームワークはfindメソッドを生成しますか、それともチュートリアルの記述でその非常に重要な部分が省略されていますか?
編集-詳細 PostsControllerというフォルダーapp/Controllerにコントローラーがあります。
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);
}
}
/ 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>
モデルは、上記の元の投稿に記載されているとおりです。
データベースには、チュートリアルで使用した次のデータがあります。
/* 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());