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:
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:
- 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' );
...
}
?>
- 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');?>
- 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!



The new file extension for views is “.ctp”, not “.cpt” as you mistakenly wrote.
Thanks Daniel for pointing out
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…..
@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
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….:)
@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
I think, Ahsan you are right.
Nice post… Enjoyed the response/comment part also..:)
Keep up..:
In my host not works with these quotes: var $helpers = array(’Form’ );
but works with these: array(“Form” );
@Pablo Gadino: Works for me. I am using the fresh code from the svn, if that has anything to do with it.
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!!!!!!)
Wow, exactly what I needed since I am starting with 1.2 and not 1.1. Many thanks!
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.
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.
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.
)
http://genewallcole.com/phpBB/viewtopic.php?p=255295#255295 a
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!
Thanks Ahsan, i ran into trouble making a blog on cakephp 1.2. but now i solved it with your article. Thanks!
regards,
Michelle
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
Just a quick correction.
3. Also change the view of edit (/app/views/posts/edit.thtml) to:
Other than that, thanks very much
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:
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:
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]
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
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
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…
solved! its work with custom css, sorry for crowding ur comments mr. ahsan
nice to surf ur blog…and gud share…im newbie in cakephp.
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.
it helps me very much.
Thank you ^-^
I have this problem too! Help please!
Hello webmaster
I would like to share with you a link to your site
write me here preonrelt@mail.ru
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 .
Great post! Very useful!
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
*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’)
);
?>