dongye9453 2013-07-18 01:37
浏览 14
已采纳

出现在cakePHP页面顶部的控制器/模型代码

I'm following the cakePHP "getting started" here:

http://book.cakephp.org/2.0/en/getting-started.html

One thing I got stuck on is that I created the model, controller, and view for Posts eg:

<docroot>/app/Controller/PostsController.php
<docroot>/app/Model/Post.php
<docroot>/app/View/Posts/index.ctp

exactly as outlined in the link above. The problem is that I was getting the error:

Error: PostsController could not be found.

And the Controller code was being printed at the top of the page.

I finally figured out that I had to add

<?php [code] ?>

around the code that the tutorial specified for the controller file. ie intstead of:

class PostsController extends AppController {
    public $helpers = array('Html', 'Form');

    public function index() {
        $this->set('posts', $this->Post->find('all'));
    }
}

I had:

<?php
class PostsController extends AppController {
    public $helpers = array('Html', 'Form');

    public function index() {
        $this->set('posts', $this->Post->find('all'));
    }
}
?>

Once I did that, it worked, kinda. Except then I was getting the model "code" printed at the top of the page. Surrounding that with the php escape (as in the controller above) resolved that issue, but I'm concerned that this is hinting that I've misconfigured something while setting up cake.

My question: is the tutorial wrong? Why would it not specify the use of the php escapes (or whatever you call those things) if they are needed to get this stuff working? Or, if they should not be needed, can anyone suggest a reason that in my case they are needed? Maybe I've mis-configured something before this step...

Thanks, I looked for a similar question on SO but was unable to locate one, but please feel free to direct me if this is a duplicate...

  • 写回答

1条回答 默认 最新

  • doumeng4400 2013-07-18 01:45
    关注

    My question: is the tutorial wrong? Why would it not specify the use of the php escapes (or whatever you call those things) if they are needed to get this stuff working?

    The tutorials expect a bare minimum of basic php knowledge. You might want to start here reading about the basic php tags. You won't get far with CakePHP if you don't know at least basic php, object oriented programming and an understanding of the OOP design pattern called MVC - model view controller.

    In a php only file you definitly need the starting tag <?php but you do not want to close it. For the reasons check this answer:

    Why would one omit the close tag?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教