31
Aug
07

Making The Blog Tutorial Run on CakePHP 1.2

If you have ever tried to learn cake, the chances are that you have already gone through the CakePHP Blog Tutorial found in the manual. It in an excellent read to get acquainted with the cake way. But unfortunately, the Blog Tutorial only runs fine in CakePHP 1.1. If you try to run the code on 1.2, you will get errors as shown below, either when you add or edit post:

Blog Tutorial Error in CakePHP 1.2

The main reason for this is that functions like input(), tagErrorMsg(), textarea(), hidden() in the Html Helper are now deprecated. Instead, all the form related functions are moved in the Form Help.

So, to make the blog tutorial code run on 1.2, you just have to make the following simple changes:

  1. Load the form helper in the Posts Controller (/app/controllers/posts_controller.php):

    <?php
    class PostsController extends AppController {
    var $name = ‘Posts’;
    var $helpers = array(’Form’ );

    }
    ?>
  2. Change the view of add (/app/views/posts/add.thtml) to:

    <h1>Add Post</h1>
    <?php echo $form->create(’Post’);?>
    <p>
    <?php echo $form->input(’title’); ?>
    </p>
    <p>
    <?php echo $form->input(’body’); ?>
    </p>
    <?php echo $form->end(’Submit’);?>
  3. Also change the view of edit (/app/views/posts/edit.thtml) to:

    <h1>Edit Post</h1>
    <?php echo $form->create(’Post’);?>
    <?php echo $form->input(’id’); ?>
    <p>
    <?php echo $form->input(’title’); ?>
    </p>
    <p>
    <?php echo $form->input(’body’); ?>
    </p>
    <?php echo $form->end(’Submit’);?>

And thats it! Your blog tutorial code will be running smoothly on 1.2. One last thing, that I recommend, is to change the extension of all the views from .thtml (e.g. index.thtml) to .ctp (e.g. index.ctp). Though not doing so will not create any problem, but you better get use to the .ctp extension, as this is the defaut file extension for CakePHP 1.2 views.

For a more complete picture of the changes in 1.2, please visit: CakePHP 1.2: The Romance Continues…

Thats it! :)


34 Responses to “Making The Blog Tutorial Run on CakePHP 1.2”


  1. 1 Daniel Hofstetter 1 September, 2007 at 5:23 pm

    The new file extension for views is “.ctp”, not “.cpt” as you mistakenly wrote.

  2. 2 Ahsan 1 September, 2007 at 6:52 pm

    Thanks Daniel for pointing out :)

  3. 3 Junal 2 September, 2007 at 12:02 am

    ahsan this issue’s example already given in “Simple form authentication in 1.2″…. http://bakery.cakephp.org/articles/view/simple-form-authentication-in-1-2-x-x

    but still thx for focusing it clearly…..

  4. 4 Ahsan 2 September, 2007 at 12:08 am

    @Junal: this post’s “issue” is not to explain how to work with form and authentication. It is simply to point out what it takes to run the Blog Tutorial on 1.2 :)

  5. 5 Junal 2 September, 2007 at 12:16 am

    well, we saw authentication and blog examples where given in CakePHP 1.1 right. they just took one example[i.e authentication] form 1.1 and showed it how to do that in 1.2. is it anymore necessary to show similar example how to run it in 1.2 [i.e blog tutorials] ? ….well still i appreciate for this findings….:)

  6. 6 Ahsan 2 September, 2007 at 12:26 am

    @Junal: You still didn’t get the point did you? The whole idea about this blog in not to explain the difference in using forms in 1.1 and 1.2. Its more about how to make the Blog Tutorial run on 1.2. Why did I bother? Because not everyone is going to start with 1.1. There might be people out there who are giving their first try with cake, using 1.2. So, I thought this will help them to use the Blog Tutorial, and also get to know little bit about 1.2, instead of first knowing how it works with 1.1 first. I hope you got the “point” now. But, I doubt it :P

  7. 7 Rubel 2 September, 2007 at 1:21 am

    I think, Ahsan you are right.

  8. 8 apache007 2 September, 2007 at 1:00 pm

    Nice post… Enjoyed the response/comment part also..:)
    Keep up..:

  9. 9 Pablo Gadino 5 September, 2007 at 11:10 pm

    In my host not works with these quotes: var $helpers = array(’Form’ );

    but works with these: array(”Form” );

  10. 10 Ahsan 6 September, 2007 at 6:07 am

    @Pablo Gadino: Works for me. I am using the fresh code from the svn, if that has anything to do with it.

  11. 11 Mo 12 October, 2007 at 6:16 am

    OMG!!!! Are you reading my mind and somehow wrote this article to magically save my day?? NO? to bad, cause you’re my hero anyway (Just read the romance article tooo: nice!!!!!!)

  12. 12 Adam 20 October, 2007 at 2:13 am

    Wow, exactly what I needed since I am starting with 1.2 and not 1.1. Many thanks!

  13. 13 tim 24 October, 2007 at 12:31 pm

    I get the following when i implement the above. –err– Undefined variable: form [CORE/app/views/posts/add.ctp, line 2 — and — Fatal error: Call to a member function create() on a non-object in….i’ve added the var $helpers = array (’Form’ ); not sure what’s going on. thanks in advance.

  14. 14 Greg 31 October, 2007 at 4:13 am

    FYI, do not copy the code. The single quotes in the above code for some reason are not the correct characters. You can replace them with regular single quotes and the code will work correctly.

  15. 15 Sarah Lewis 15 November, 2007 at 10:52 pm

    Tim, I ran into that problem and eventually figured out it was because I was using the 1.1.x Stable release instead of the 1.2.x Pre Beta release. (This may or may not help you, but that’s what fixed my issue. :) )

  16. 16 Hillary 18 November, 2007 at 9:30 pm
  17. 17 Jeff 6 December, 2007 at 12:06 am

    Many thanks for pointing me in the direction of this. I was just dipping into Cake for the first time and whipping through all sorts of errors. One thing I notice that you idn’t provide a 1.2 alternative for is the lines with the tagError (tagErrorMsg(’Post/body’, ‘Body is required.’) ?>).

    Is there a new way to handle those?

    Anyway, thanks again and bookmarking this site as you have some fantastic resources here!

  18. 18 Michelle 7 December, 2007 at 5:11 am

    Thanks Ahsan, i ran into trouble making a blog on cakephp 1.2. but now i solved it with your article. Thanks!

    regards,
    Michelle

  19. 19 topan 18 December, 2007 at 9:54 am

    I’m using bake, with ‘cake bake’, its very easy…. and and nothing error to making blog tutorial with CakePHP 1.2
    you must try it…:D

  20. 20 Mark 6 January, 2008 at 11:58 pm

    Just a quick correction.

    3. Also change the view of edit (/app/views/posts/edit.thtml) to:

    Other than that, thanks very much

  21. 21 Mark 7 January, 2008 at 12:00 am

    hmmm, that diff didnt work….I guess that this is messing with html

    \ 3. Also change the view of edit (/app/views/posts/edit.thtml) to:

  22. 22 Mark 7 January, 2008 at 12:01 am

    i give up.

    The line should be changed from

    3. Also change the view of edit (/app/views/posts/add.thtml) to:

    TO

    3. Also change the view of edit (/app/views/posts/edit.thtml) to:

    :-) M

  23. 23 Tim 12 January, 2008 at 5:08 pm

    I just started, like you guess with cakeblog on cake 1.2. Until I replaced the copy-and-pasted quotes with regular single quotes I got error messages like the one below. Please put a comment in the code to change the quotes to regular ones so other people don’t have the problem. Thanks for writing the article :)

    Notice (8): Use of undefined constant �Post� - assumed ‘�Post�’ [APP\views\posts\add.ctp, line 4]

  24. 24 Pensae 13 January, 2008 at 1:50 pm

    Ahsan, thanks for nice article. I follow your tips and it’s work fine, but I make a note that I do not inlcude line : ” var $helpers = array(’Form’ ); ” in posts_controller.php and it still work properly, I use CakePHP Beta: 1.2.0.6311

  25. 25 Kunthar 2 February, 2008 at 1:41 am

    I wonder when, some cool cake developer will be made his/her blog with cakephp and started to publish some cool articles on it, hehe :>
    WordPress anywhere :)

  26. 26 kasmo 11 February, 2008 at 11:59 pm

    btw, how do i set a html attribute with new form helper? i was trying sumthing like this

    input(’username’,array(’size’=>’40′);
    ?>

    but its not work dude…

    advice pls…

  27. 27 kasmo 12 February, 2008 at 12:36 am

    solved! its work with custom css, sorry for crowding ur comments mr. ahsan :D nice to surf ur blog…and gud share…im newbie in cakephp.

  28. 28 CRP 16 March, 2008 at 6:44 pm

    I am a .Net developer just trying out CakePHP 1.2 for a personal website. Trying to get this sample working on my Windows box with PHP/Apache/Mysql, the Cake is not able to find the Helpers.

    I get error:
    Error: The helper file app\views\helpers\form.php can not be found or does not exist.

    I find all the helper php files in folder C:\xampp\htdocs\Cake\cake\libs\view\helpers

    I tried copying them to C:\xampp\htdocs\Cake\app\views\helpers
    But of no use.

    I cannot figure how to get PHPCake to find the form.php

    Any help will be appreciated.

  29. 29 samyaekinter 7 April, 2008 at 1:43 pm

    it helps me very much.
    Thank you ^-^

  30. 30 Mixa 15 July, 2008 at 5:31 am

    I get error:
    Error: The helper file app\views\helpers\form.php can not be found or does not exist.

    I find all the helper php files in folder C:\xampp\htdocs\Cake\cake\libs\view\helpers

    I tried copying them to C:\xampp\htdocs\Cake\app\views\helpers
    But of no use.

    I cannot figure how to get PHPCake to find the form.php

    Any help will be appreciated.

    I have this problem too! Help please!

  1. 1 cakebaker » Blog tutorial for CakePHP 1.2 Pingback on Oct 3rd, 2007 at 1:08 pm
  2. 2 CakePHP Resource collection for newbie. « life and technology in general Pingback on Oct 9th, 2007 at 2:17 pm
  3. 3 Pensae.com – ordinary user of cakephp – » Blog Archive » Cake Blog Tutorial: sebuah penyesuaian untuk CakePHP 1.2 Pingback on Jan 13th, 2008 at 3:21 pm
  4. 4 gangaftagley.com weblog » Blog Archive » Cake is starting to suck ass Pingback on Jan 27th, 2008 at 3:31 pm

Leave a Reply