Curious Rami

Entries categorized as ‘CakePHP’

CakePHP Main Page Caching Workaround

July 24, 2007 · 3 Comments

CakePHP has a great caching helper with one annoying bug; the main page located at “/” never gets cached because the cache helper tries to name the cache file .php (null) and fails.

SiteAMonth (http://www.siteamonth.com/archives/2007/02/21/cakephp-cache-workarounds-part-1/) has found a solution with a little problem:

This does generate an extra request due to the redirect. The optimal solution is to fix this in the CakePHP core. Once I have a better understanding of the cache code I may suggest a fix, assuming someone doesn’t beat me to it.

Here is my solution which requires modifying two files in CakePHP framework build cake_1.1.16.5421:

On line 206 of \cake\libs\view\helpers\cache.php change:

if (empty($cache)) {
  return;
}

To:

if (empty($cache)) {
  /*Modified by Rami Nasser */
  /*”rami” is the main page cache file name*/
  $cache = “rami”;
  /*return;*/
}

On line 90 of \cake\bootstrap.php

After:

if (empty($uri)) {
  $uri = setUri();
}

Add:

/*Added by Rami Nasser*/
if($uri == “/”) {
  /*”rami” is the main page cache file name*/
  $uri = “rami”;
}

I used “rami” for the cache file name because… Anyways make up your own.

Let me know what you think.

Categories: CakePHP

Social Bookmarking for Engineers

May 14, 2007 · 4 Comments

EngBeat.com

I believe that there is a huge opportunity for engineering related social networks. The only engineering social website that I know off is http://octopart.com/, a very useful search engine for electronic parts (let me know if you know off other social engineering websites).

Today I developed a new social bookmarking site for engineers, EngBeat: http://www.engbeat.com. I say today because it took me 4 hours to design and develop EngBeat using CakePHP. EngBeat is very beta, right now you can only submit engineering links but I intent to add accounts, categories, rss feeds and comments soon.

Give it a try and let me what you think.

Categories: CakePHP · Engineering · Science · Startup · Web 2.0

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