Archive for the 'The Blog Tutorial' Category

31
Aug

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