duanlu8613 2013-07-27 15:58
浏览 19
已采纳

Cakephp博客教程错误PostsController缺失

I am having a problem with Cake PHP blog tutorial for beginners. I got an error stating I need to create a PostsController.php file, when trying to open the site.com/posts/index page, but I already have the PostsController.php in place.

I am using cake PHP ver 2.3.8

This is the error I get

Error: PostsController could not be found.

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

I've followed the tutorial to the letter, and put the files correctly in the directory.

Here is the PostsController.php file I created.

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

Here is the Post.php file (the model)

<?
class Post extends AppModel {
}
?>

and here is the index.ctp file

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

I've already googled around for the solution and found out that a lot of people is having the same problem, but It's either their solution is not working for my case or they fixed it by themself and didn't post the solution.

Please help me...

UPDATE

the issue has been resolved, it was the <?php tag that was missing.

  • 写回答

2条回答 默认 最新

  • dqajyxqem115006813 2013-07-27 16:16
    关注

    The problem is because you are using <? instead of <?php to open your php. You can either use full tag to open php or enable short tags using php.ini.

    It's been recommended not to use the short tag "short cut" and instead to use the full tag combination. With the wide spread use of XML and use of these tags by other languages, the server can become easily confused and end up parsing the wrong code in the wrong context. Also short tags may not be supported on the target server

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题