1

我对初学者的 Cake PHP 博客教程有疑问。在尝试打开 site.com/posts/index 页面时,我收到一条错误消息,指出我需要创建 PostsController.php 文件,但我已经有了 PostsController.php。

我正在使用蛋糕 PHP 版本 2.3.8

这是我得到的错误

Error: PostsController could not be found.

Error: Create the class PostsController below in file: app\Controller\PostsController.php

我完全按照教程进行操作,并将文件正确地放在目录中。

这是我创建的 PostsController.php 文件。

<?
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);
    }
}
?>

这是 Post.php 文件(模型)

<?
class Post extends AppModel {
}
?>

这是 index.ctp 文件

<!-- 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>

我已经在谷歌上搜索了解决方案,发现很多人都遇到了同样的问题,但是要么他们的解决方案不适用于我的案例,要么他们自己修复了它并且没有发布解决方案。

请帮我...

更新

问题已解决,是<?php标签丢失了。

4

2 回答 2

4

问题是因为您使用<?而不是<?php打开您的 php.ini 文件。您可以使用完整标签打开 php,也可以使用php.ini.

建议不要使用短标签“short cut”,而是使用完整标签组合。随着 XML 的广泛使用和其他语言对这些标记的使用,服务器很容易混淆,并最终在错误的上下文中解析错误的代码。目标服务器也可能不支持短标签

于 2013-07-27T16:16:11.767 回答
0

这是我创建的 PostController.php 文件。

控制器文件的名称是PostsController.php- 因为找不到类而发生错误。

于 2013-07-27T16:00:08.363 回答