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

About these ads

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


  1. 1 September, 2007 at 5:23 pm

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

  2. 1 September, 2007 at 6:52 pm

    Thanks Daniel for pointing out :)

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

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

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

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

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

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

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

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

  24. 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 :)

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

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

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

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

    it helps me very much.
    Thank you ^-^

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

  30. 31 Alexwebmaster
    3 March, 2009 at 3:18 pm

    Hello webmaster
    I would like to share with you a link to your site
    write me here preonrelt@mail.ru

  31. 4 June, 2009 at 10:47 am

    Sweet blog. I never know what I am going to come across next. I think you should do more posting as you have some pretty intelligent stuff to say.

    I’ll be watching you . :)

  32. 33 cervatz
    17 September, 2009 at 7:35 pm

    Great post! Very useful!

  33. 34 rax
    30 October, 2009 at 1:52 pm

    I am having a problem displaying the index page of the blog tutorial.
    Has anyone encountered the errors below.

    Notice: Undefined variable: posts in C:\wamp\www\cake_1.2.5\app\views\posts\index.ctp.php on line 13

    Warning: Invalid argument supplied for foreach() in C:\wamp\www\cake_1.2.5\app\views\posts\index.ctp.php on line 13

    Notice: Undefined variable: html in C:\wamp\www\cake_1.2.5\app\views\posts\index.ctp.php on line 26

    Fatal error: Call to a member function link() on a non-object in C:\wamp\www\cake_1.2.5\app\views\posts\index.ctp.php on line 26

    Thanks for your help.
    Rax

  34. 13 November, 2010 at 6:51 pm

    *Notice (8): Use of undefined constant VALID_NOT_EMPTY – assumed ‘VALID_NOT_EMPTY’ [CORE\cake\libs\class_registry.php, line 141]
    *Warning (2): preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash [CORE\cake\libs\model\model.php, line 2577]
    ————————————————————————————–
    array(‘rule’=> VALID_NOT_EMPTY,
    ‘message’ =>’Produit can Not be empty’
    ),
    ‘N_produit’ =>array(‘rule’=> VALID_NOT_EMPTY,
    ‘message’ =>’N produit can Not be empty’
    ),
    ‘prix’ => array(‘rule’=> VALID_NOT_EMPTY,
    ‘message’ =>’Prix can Not be empty’
    )
    );
    }
    ?>
    ———————————————————————–
    views
    ——-
    create(‘Livre’);
    ?>

    ajouter nouveau Produit
    input(‘produit’);
    echo $form->input(‘N_produit’);
    echo $form->input(‘prix’);
    echo $form->input(‘date’);
    ?>

    end(‘Ajouter’);
    echo $this->Html->link(
    ‘acueil’,
    array(‘controller’=>’livres’, ‘action’=>’index’)
    );
    ?>


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


NOTICE:

I no longer maintain this blog. Please visit my new blog at http://ahsanity.com

My Book on CakePHP

If you are looking for a book to get started with CakePHP, then this book is definitely what you need :)
10% off at the publisher's site.

Blog Stats

  • 159,496 hits

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: