For beginners: famous pitfalls with cake development

by alexdd55
Starting from scratch in cakephp is not always easy, especially when you are not used to MVC frameworks. I hope this will help other beginners to make their life easier ;-)

Incorrect filenames

It happens to me over and over again, writing a filename with or without an "s" at the end, in code and as a filename also. This drives me crazy, because it takes hours and hours to find this mistake, if you only watch the code.

Try to stick with the conventions:
Contollers are always plural, e.g. users_controller.php
Models are singular, e.g. user.php

Remember to use "echo" in views

Building a form with the form helper and it isn' there... very anoying. So check first if you use echo on all relevant lines ;-)

I try to use plain html instead of php embedded html, so i have to write

<div>
<?php echo $some['array']; ?>
</div>
instead of

<?php
echo '<div>';
echo 
$some['array'];
echo 
'</div>';
This helps also in IDEs like Netbeans with codeformating, which will also raise productivity.


Correct paths to images

You will experience problems, when you do not use the HtmlHelper for images in your views.
The HtmlHelper will always create the right paths to the pictures, no matter where the site in your structur is found. Using a standard html img tag will often lead to a problem in your view, so the images will not be shown.

Links
HtmlHelper: http://book.cakephp.org/view/835/image

Unexpected results in ajax calls

This can be a pain in the back. All logic is fine, but the result is not that what you expected?
I know.. happens almost everytime to me, after creating a new method.
So, check your debug level. It can mess up the result.

function foobar()  {
   configure::write('debug',0);
   // somthing happens here
}
This will help a lot and will save time.

Nice urls

With thinking about SEO and nice urls, so a lot of people (incl. me in the beginning) try to give controllers and actions a name that will look great in the addressbar.
[p]Avoid that until you know what you are doing
Take what the database and the name convention gives you, just make everything work first and do not worry about the nice urls.
In the beginning i spend almost half of my time with thinking about names and stuff, don't waste your time and use the html-helper for links, like you are supposed to do, and later, when you are done coding, routes will do everything for you.
You will be surprised how great it works, because the routing will change all related links automatically, when the links are build with the html-helper

Links
HtmlHelper: http://book.cakephp.org/view/206/Inserting-Well-Formatted-elements#link-836
Routing: http://book.cakephp.org/view/46/Routes-Configuration

Put the query where it belongs

In the begining i always put my query in the controller, because it was an easy way to get what i want and it didn't bother me in any way.
After getting used to the cakephp, i found out that it was just stupid, because the structure won't stay clean.
To stay on top of things, put all that stuff in the related model and get the data from there.
The controller will stay lean and thats what you want... just believe it.
If your projects starts to get more complex, you will be thankful that you did it that way ;-)


...and always remember...

No matter what you are about to do, this is still PHP and you CAN do almost everything you want,everywhere.
But be careful with that, because getting too far away from the way you are supposed to do things, could make you rework your "lazy" code again, after having some weeks experience with developing in cake.

Report

More on General Interest

Advertising

Comments

  • sachy posted on 02/03/11 12:07:39 AM
    Hi alexdd55,
    thank you so much for your great article.
    I found this is very useful and let Japanese CakeBaker know about this.
    Google's auto translation is something broken, so I made Japanese translation on my blog.
    http://bakery.cakephp-users.jp/2011/02/03/for-beginners-famous-pitfalls-with-cake-development/
  • pere posted on 01/28/10 06:48:02 PM
    hey, as already demystified by alex, here is the corresponding chapter in the cook book:

    http://book.cakephp.org/view/72/Additional-Methods-and-Properties
  • pere posted on 01/20/10 05:10:50 PM
    Hey, i'm kind of new to cake but i'm already developping kind of a big web application with it. now i stumbled upon this article and had to grin about well-described pittfalls that i did and still do step into. however the "Put the query where it belongs" hint didn't ring a bell. I look through the whole cook book to see if i missed a crucial hint there, but i found nothing.
    (1) how do you put a query in the model? by defining a function as a method of the model which takes certain crud conditions as a parameter?

    (2) in which way would that be any more clear or anything?

    i really don't get it. even in the cook book blog example (yeah, kinda lame) they "put the query" in the controller.....

    Were you after all just talking about using model associations instead of making manual joins via this->query in the controller?
    if so than this hint is very confusing.

    would appreciate your answer!!!

    pere
    • alexdd55 posted on 01/26/10 03:08:05 AM
      (1) how do you put a query in the model? by defining a function as a method of the model which takes certain crud conditions as a parameter?
      exactly. thats the way to keep the controller clean and lean. :)

      (2) in which way would that be any more clear or anything?
      this is preservation of MVC principles. you just don't want this kind of stuff in the controller.

      i figured out that the "Containable"-Behaviour is very helpful to get the data i need, without making my code more complicated.

      i hope this will help to answer your questions.

      for manual queries, check out:
      http://book.cakephp.org/view/456/query
      for the "Containable"-Behaviour, check out:
      http://book.cakephp.org/view/856/Using-Containable
  • michaelc posted on 11/05/09 02:19:25 PM
    These are indeed some common pitfalls that people, beginners and experienced alike, should know more about. I can't express how many times such simple errors bite people getting started in CakePHP - this is good advice. As an aside, you might want to consider the essay throughout your life, @http://www.tomorrowlands.org/humor/essay.html
  • TheVirus2013 posted on 10/29/09 05:39:24 AM
    This is a very good article. I experienced exactly the same problems as you when I started using CakePHP, and it's right that it takes sometimes a lot of time to discover what you've not done correctly.
    Good work ! ;-)
  • gaioshin posted on 10/28/09 04:50:55 AM
    Excellent article! To add on the list:-


    * As a further point to the part about filenaming, also be aware of when you should use camelBacking and when_to_join_with_underscores - read the CakePHP coding conventions!

    * Database naming - remember, tables associated with models should be pluralised

    * AJAX - if you are trying to get beautiful UI transitions within your views and you're using AJAX learn to use basic FireBug with Firefox

    * Be aware of whats being passed to the views and what it contains - one of the super quick, super dirty newbie techniques I use is simply to put pr($this) sometimes in the view if I keep getting variable headaches


    ...of course, there are many more but they are the ones that come to mind first!
  • simwol posted on 10/11/09 10:09:55 AM
    It took a while but then I realised there must be no 's' when my object is named 'media'.

    And make sure to use 'id' (case-sensitive) and 'name' database fields.

    best regards
    Simon
  • ojtibi posted on 10/04/09 11:45:02 PM
    Well said with your article, specially the nice URL/controller name sections. Unfortunately, my project cannot handle the links using the core HtmlHelper::link() method, since I use subdomains with the URLs. A possible approach to my problem, though, is a custom subclass of the HtmlHelper.

    Cheers!
  • PotterSys posted on 10/02/09 02:30:38 PM
    Nice guidelines, but I didn't understand what is the goal on "Put the query where it belongs" and how to achieve it =/
    • alexdd55 posted on 12/04/09 05:38:14 PM
      Nice guidelines, but I didn't understand what is the goal on "Put the query where it belongs" and how to achieve it =/
      this means, that sometimes you might get lazy and put a query like $this->Modelname->find('all',...'conditions' => array(....))  into your controller, but this should be placed into the model.
  • robwilkerson posted on 09/29/09 07:35:51 AM
    The Ajax pitfall killed me every single time until I made the addition of the following code a standard part of my setup for a new CakePHP project:

    Controller Class:

    <?php 
    class AppController extends Controller {
        if ( 
    $this->RequestHandler->isAjax() ) {
            
    Configure::write 'debug');
        }
    }
    ?>

    This saved a bit of my sanity.
login to post a comment.