CakePHP notes

Cake PHP is a PHP based MVC web schema based on Ruby on Rails. In other words, Cake is a rapid development engine for PHP modeled after Ruby on Rails.

For some reason I began looking at CakePHP before Ruby on Rails. The next day I installed and played with RoR, and all I can say is Wow!. Since I am a ruby fan anyway, it would seem clear that ruby on rails is the way to go. The flip side of this enthusiasm though is the hassle involved in getting ruby on rails to play nice with an Apache 2.0 server, which is a no-brainer with PHP.
Here is a quote by Robert Cooper from an article on rails, ruby and java:

Rails is great in a lot of important ways. In fact, if the Flying Spaghetti Monster that PHP is would die and be replaced by Ruby, I think that would be a huge step forward.

I imagine there will be considerable interest in cake anyway, since there are lots of PHP savvy web hacks out there who will balk at using ruby. The will do more than balk at the apache issues. Ruby on rails seems so good though, that many people just dump apache and run lighttpd instead. I will be curious to see just how PHP compares with ruby-rails-fastcgi as far as scaling for a high volume site.

In June of 2006, I tried to work my way through the Cake tutorial, and these notes tell you how it all went. I am running on a Fedora Core 4/5 system with an Apache web server.

First, visit the CakePHP site and download the latest release (for me this was 1.1.5.3148). Also read or at least admire their documentation while you are there.

Installing it seems to boil down to untaring the distribution into DocumentRoot/cake. When I tell my browser to to to http://cake I get the error: 500 Internal Server Error so I have some issue to work out now with Apache. In fact when I look at the apache error_log it tells me:

[alert] [client 128.196.100.99] /www/main/cake/.htaccess: RewriteEngine not allowed here

What I next did, attempting to follow the suggestions in the tutorial, is to add the following 3 lines to my /etc/httpd/conf/httpd.conf file, then doing service httpd restart

<Directory /www/main/cake>
    AllowOverride All
</Directory>

Yep, this seems to do it -- probably could just copy the junk in the .htaccess file right into httpd.conf, but we will leave it like this, and ponder the fact that we should spend a day sometime studying the apache configuration directives.

Now the thing to do is man-handle MySQL and create a database, with the proper table, and a MySQL user or two for this little project. See my MySQL notes for detail on this often neglected and tricky aspect of a project like this. (Fighting with MySQL and Apache config are the nasties parts of dealing with Cake ... so far.)

I get pretty far along through the tutorial until the next bit of trouble crops up. Cake wants to be able (as user apache) to create and write files into DocumentRoot/cake/app/tmp. What I do is:

cd app/tmp
chgrp -R apache *
chmod -R g+w

This gets rid of several error messages.

To add messages to this thing I use the URL:

http://www/cake/posts/add

To see what is there I use the URL:

http://www/cake/posts

Bugs

As of 6-27-2006, there are a number of bugs in the tutorial code ... not that I want to complain, but it is so important that tutorials get it right. On the other hand, finding a fixing a few bugs help teach the concepts, and build confidence ... in your own abilities if not in the product you are working with!! Also I would say this 15 minute business is utter nonsense, maybe an hour or two ... if you don't count the hassles with Apache and MySQL. If someone has to learn MySQL before starting, I would expect several days to get up and running.

What bugs you ask, I have been fixing and forgetting them as I go along, but here are two that I still remember:

The form in edit.thtml created a URL to posts/add rather than posts/edit, so the form line should be changed to
<form method="post" action="<?php echo $html->url('/posts/edit')?>">
The Add Post URL in index.thtml is best generated via the link method as follows:
<?php echo $html->link("Add Post", "/posts/add" ); ?>
On my server I was running this under /cake/posts, so this made it work instead of falling on its head.

Analysis

So, what can we say after blazing through the tutorial? We have created a functional web application with a MySQL backend with fairly little effort, and without even mentioning SQL in the php code. (We do need to do the usual hassle to get the database setup.)

We created a database cakeblog with a single table called posts. The table contains the five columns:

CREATE TABLE posts (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(50),
    body TEXT,
    created DATETIME DEFAULT NULL,
    modified DATETIME DEFAULT NULL
);

We wrote 6 files (by my count):

models/post.php
controllers/posts/controller.php
views/posts/index.thtml
views/posts/add.thtml
views/posts/view.thtml
views/posts/edit.thtml

We have methods to fiddle the database:

$this->Post->findAll()
$this->Post->read()
$this->Post->save()
$this->Post->del()
$this->Post->set()
$this->Post->flash()

We access stuff from a database row via:

foreach ( $posts as $post ) ...
    $post['Post']['id']
    $post['Post']['title']
    $post['Post']['body']
    $post['Post']['created']

We can set stuff in a database row via:

$this->Post->id = 25;
$this->data['Post']

We can generate html form elements and hyperlinks via:

$html->url()
$html->link()
$html->hidden()
$html->input()
$html->textarea()
$html->submit()

Of course none of this makes sense unless you work through the tutorial and/or read the online Cake manual.


Feedback? Questions? Drop me a line!

CakePHP notes / tom@mmto.org