Curious Rami

Learning CakePHP Step 1

April 22, 2007 · 13 Comments

This is a tutorial on how to design a website from scratch using CakePHP. To make the tutorial useful and challenging, I decided to clone an existing web application. One web app that I visit every day is http://news.ycombinator.com/. Y.News is a great resource for startups and it is based on Reddit.com.

To accelerate the development cycle, I copied Y.News’s .css file and HTML structure.

All that is left is the dynamic code (php) including database connection, user authentication, etc…

I first started by designing the database and followed CakePHP naming conventions. I have four tables: users, sites, comments and votes. ‘Users’ has many sites, comments, and votes; and ‘sites’ has many comments and votes.

Then I ran cakePHP’s bake.php to build the default models, controllers, and views.

Then I designed the website structure:
/sites/newest
/sites/best
/sites/submit
/sites/view
/users/add
/users/login
/users/view
/layouts/default
/errors/error404

To make the website more efficient, I added two fields, commentscount and votescount, to the sites table and updated those fields every time a new comment or a new vote is added. This saved me two additional JOIN queries.

Then I deleted all unnecessary model relationships, controller functions, and views. Most importantly, is deleting unnecessary model relationships, for example, the relationship between the votes table and the sites table is no longer needed after adding the votescount field to the sites table. Deleting relationships saves you a JOIN query.

Sometimes you will be forced to add a JOIN query. For example, /sites/view displays one site and all related comments, to show the username of the comment submitter, you will need to add a new query:

Sites.*, Comments.*, Users.username AS Auther FROM Comments INNER JOIN Users ON Users.id = Comments.user_id LEFT OUTER JOIN Sites ON Comments.site_id = Sites.id WHERE Sites.id = ‘. $id

This website was built in 4 hours; that is a lot given that this is a simple website. CakePHP is awesome if you are familiar with the API, if not it might be a bit frustrating to learn it all, but trust me it is all worth it. For example, to display the time elapsed since a site was added, you could use: $time->relativeTime($site['Site']['created']), now that is awesome!

Here are few screenshots and the source code:

reddit-1.gif

reddit-2.gif

reddit-3.gif

reddit-4.gif

Code:cakephp-tutorial-projectrar.pdf (right click, save as, rename extension to .rar)

SQL: cakephp-tutorial-projectsql.pdf (right click, save as, rename extension to .sql)

Categories: Blogging · CakePHP · Software · Startup · Web 2.0

13 responses so far ↓

  • Ahsan // April 22, 2007 at 11:42 pm

    Nice and simple tutorial. Though still didnt have the time to go through the code, but looks good :)

  • Shane // April 23, 2007 at 4:15 pm

    Hi,

    I was able to download the php code but the sql file has nothing in it!

  • Rami Nasser // April 23, 2007 at 4:19 pm

    Hey Shane, it works for me, but just incase here it is:

    CREATE TABLE `users` (
    `id` int(10) NOT NULL auto_increment,
    `username` varchar(50) NOT NULL,
    `password` varchar(40) NOT NULL,
    `email` varchar(255) NOT NULL,
    `created` timestamp NOT NULL default CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `username` (`username`,`email`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

    CREATE TABLE `sites` (
    `id` int(10) NOT NULL auto_increment,
    `user_id` int(10) NOT NULL default ‘0′,
    `title` varchar(255) NOT NULL default ‘0′,
    `url` varchar(255) NOT NULL default ‘0′,
    `description` text,
    `created` timestamp NOT NULL default CURRENT_TIMESTAMP,
    `commentscount` int(10) NOT NULL default ‘0′,
    `votescount` int(10) NOT NULL default ‘0′,
    PRIMARY KEY (`id`)
    ) ENGINE=MyISAM AUTO_INCREMENT=30 DEFAULT CHARSET=latin1;

    CREATE TABLE `votes` (
    `id` int(10) NOT NULL auto_increment,
    `site_id` int(10) NOT NULL default ‘0′,
    `user_id` int(10) NOT NULL default ‘0′,
    `created` timestamp NOT NULL default CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`)
    ) ENGINE=MyISAM AUTO_INCREMENT=47 DEFAULT CHARSET=latin1;

    CREATE TABLE `comments` (
    `id` int(10) NOT NULL auto_increment,
    `site_id` int(10) NOT NULL default ‘0′,
    `user_id` int(10) NOT NULL default ‘0′,
    `description` text NOT NULL,
    `created` timestamp NOT NULL default CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`)
    ) ENGINE=MyISAM AUTO_INCREMENT=109 DEFAULT CHARSET=latin1;

  • JMC // April 26, 2007 at 4:25 pm

    hmmm… where is the tutorial in your “tutorial”? i guess this isn’t one for beginners to cakephp.

  • Rami Nasser // April 26, 2007 at 4:39 pm

    For beginners I recommend:
    http://manual.cakephp.org/chapter/scaffolding
    http://manual.cakephp.org/appendix/blog_tutorial
    http://manual.cakephp.org/appendix/simple_user_auth

  • Tengku Zahasman // April 29, 2007 at 12:18 pm

    CakePHP is awesome.. but it really takes a while in learning before we get the hang of it. Does anyone know where can I find a directory of cakephp-based applications beside those listed in the cakephp google groups?

  • Rami Nasser // April 29, 2007 at 12:27 pm

    Hey Tengku, try the following projects from cakeforge.org:

    http://cakeforge.org/projects/timesheet/
    http://cakeforge.org/projects/cheesecake/

    Also try:
    http://www.simply-done.info/

  • sonic // June 1, 2007 at 2:27 am

    Would you like to show me how to configure CakePHP at xampp? Thanks in advance.

  • Rami Nasser // June 1, 2007 at 2:47 am

    XAMPP includes Apache web server, MySQL, and PHP; that all you need to run CakePHP. You need to edit database.php in app\config folder.

  • sonic // June 1, 2007 at 5:46 am

    Cake is already run at my XAMPP. I test Cake Tutorials notes. I can’t call http://localhost:7070/notes/ [ here I have to say my Apache port is 7070]. If I call http://localhost:7070/cake/, I saw the messages “Your database configuration file is present.

    Cake is able to connect to the database. ” .

    So How can I call notes? If I call that , error “Object not found!” . Please!

    Thank in advance!

  • Rami Nasser // June 1, 2007 at 9:51 am

    Verify that you have an index.thtml in folder: app\views\notes\

    verify that your apache server is configured as described in section 5 “Configuring Apache and mod_rewrite” http://manual.cakephp.org/chapter/installing

  • Tahmeed // June 19, 2007 at 6:51 am

    I dont know why it wont bake or is not able to open is there anyway how to solve this problem.

    C:\xampp\php>php bake.php -c:\xampp\htdocs\hramil\cake\
    Could not open input file: bake.php

    Tahmeed

  • Seandy // August 11, 2007 at 11:35 pm

    Hi… Rami, nice tutorial,
    i’m glad to find your site, i’m from indonesia.
    nice to meet u.

    please keep posting cakephp tutorials, i’m a big fan of it.

    Thanks.

You must be logged in to post a comment.