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 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)