0

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

snuggles
  • 318
  • 2
  • 4
  • 12
  • Just to be clear: I *do* know php/OO/MVC basics. My confusion is rooted in the fact that the tutorial presents the code blocks as if they were the entire file contents. As you must add the php tag around the code, that is clearly not the case. I just want to be certain that there isn't some configuration I have set incorrectly that would allow the files to be interpreted *without* the php tags. – snuggles Jul 18 '13 at 10:56
  • There is no such option as php was intended to be used together with HTML in the same file when it was new. Today you want to separate the view (HTML) from the business logic. Also see http://stackoverflow.com/questions/4410704/why-would-one-omit-the-close-tag – floriank Jul 18 '13 at 12:40
  • Yes I can see that I made the mistake of thinking this would be easy since I already use perl and python. But your point about how it was intended to be used is well taken. Thanks! – snuggles Jul 19 '13 at 19:33

1 Answers1

3

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?

Community
  • 1
  • 1
floriank
  • 25,546
  • 9
  • 42
  • 66
  • Well, the problem is that the tutorial presents the code as complete, with no indication that it's a sub-set of a larger block. There is no reason to just *assume* that php tags should surround the code presented in the tutorial. It seems rather strange to me that these tags would not be included in the code samples. Even if that is bog-standard php, the fact is that php *could* read those files as plain text and compile them without the tags, so I'm not convinced that my confusion is unwarranted. – snuggles Jul 18 '13 at 10:49
  • Part of my comment to you is wrong: there *is* reason to assume that the php tag should start any php file. I still think the cake tutorial should not present what is basically incomplete code (for example, when you present a perl file example, most people include the bang[your perl install here], so it seems weird that would not be the case for php), but I accept that this is a language conceit and I thank you for your answer. – snuggles Jul 18 '13 at 11:31