我在Cake PHP初学者博客教程中遇到问题。当我试图打开site.com/posts/index页面时,我遇到了一个错误,说我需要创建一个PostsController.php文件,但我已经安装了PostsCoontroller.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
标签。
问题是因为您使用<?
而不是<?php
来打开php。您可以使用完整标签打开php,也可以使用php.ini
启用短标签。
建议不要使用短标签"shortcut",而是使用完整的标签组合。随着XML的广泛使用和其他语言对这些标签的使用,服务器很容易混淆,最终会在错误的上下文中解析错误的代码。此外,目标服务器上可能不支持短标签
这是我创建的PostController.php文件。
控制器文件的名称为PostsController.php
-由于找不到类而发生错误。