Piece of Cake

Well, folks, I’m back from Summer Film with a vengance. And it’s time to get down to some programming, so here goes.

We’ve all heard about Ruby on Rails. “It’s so great,” they say. “Ruby is so simple, and efficient,” I hear. “There’s a cool guide that has Foxes!” says some other guy from the back.

But I already know PHP. I like PHP. In fact, I really like PHP. It has that old-school C style syntax, and yet, it’s just frustratingly different enough from C to make it enjoyable to code in. (I use 4; 5’s good too, but I haven’t bothered to seriously update, because my hosts still use 4 on the majority of their boxes, including the one I’m on.)

So what great MVC package is there for PHP similar to Rails for Ruby? Well, there’s PHP on Trax. A plus is that it fairly closely follows Rails. But, it requires PHP 5. Hmm. Yeah. 5’s great, but no thanks right now.

There’s also TaniPHP, which supports PHP 4. But that’s not been updated in ages, and it looks like it won’t be.

And then there’s CakePHP. PHP 4 support, check. Inspired by Rails, check. Good documentation, check. Active community, check. Fantastic.

So here’s my adventure installing CakePHP on Mac OS X. You can follow along, if you like.

Step Zero:
Install PHP and MySQL for OS X. I did this a long time ago, but if you’re just getting started, you’ll probably like to do this. Otherwise, things are really not going to make sense later. There are great instructions for installing PHP & MySQL by hand on Apple’s Developer Connection, but I’m not setting up a production box, I’m setting up a development box. For that, I do things the easy way. I’ll optimize later.

So here’s the easy way: Marc Liyanage’s OS X Packages. Easy, helpful, just follow his instructions.

Step One:
Download and unzip Cake. Because I’m developing multiple Cake apps, I’m going to put the resulting directory in my /Library (that’s Root Library. Not the User or System Library.) On most Unix systems, a shared Cake install goes to /usr/lib.

The latest stable version at the time of this writing is 1.1.5.3148. Go ahead and download it from CakeForge. Once you’ve unzipped it, rename the resulting folder “Cake” and move Cake to /Library. Now you’ll have your Cake Libraries installed in /Library/Cake. Recursively delicious.

Step Two:
It’s likely you want to work on your Cake application somewhere other than /Library. And you probably don’t feel like modifying httpd.conf either. So good news – you can simply move the /Library/Cake/app folder wherever you feel like working – and name it whatever you like. You can also duplicate this folder and create multiple Cake apps. I simply put it in my home directory. Thus, I end up with /Users/myhomedir/app. If I want to start a blog application (like the tutorial app, for example) I’ll duplicate that folder and rename it ‘blog.’ I can duplicate this folder over and over to create my applications.

Step Three:
Now that you have your library and applications folders in place, you need to move Cake’s webroot directory into the place where you want to serve your files from. For example, when you activate Personal Web Sharing, the Apache serves files from /Library/WebServer/Documents. If I move /Users/myhomedir/app/webroot to /Library/WebServer/Documents/webroot, then my Cake application will be accessible at http://localhost/webroot/. If I copy the contents of app/webroot, to /Library/WebServer/Documents, then my Cake app will simply serve out of http://localhost.

I like working out of my home directory, though. Fortunately, Personal Web Sharing also enables the Sites folder of my home directory to serve files: http://localhost/~myhomedir/. Using the blog example, I’ll typically move the webroot folder (in /Users/myhomedir/app) to /Users/myhomedir/Sites, and then rename it ‘blog.’ Now, my Cake blog will be served from http://localhost/~myhomedir/blog/. Flexible, and handy.

Step Four:
Now all your files are in place. To give Cake a working install, though, you’ll need to tell it where your libraries and app files are!

Going back to the blog example, let’s review:
My Cake Libraries are in /Library/Cake.
My Application Folder is /Users/myhomedir/blog.
My Application Webroot is /Users/myhomedir/Sites/blog.

First, modify the index.php file in your webroot. Look for the comment box that talks about Cake being installed in a directory layout different from it’s distribution. Modify the code like so:

/**
* These defines should only be edited if you have cake installed in
* a directory layout other than the way it is distributed.
* Each define has a commented line of code that explains what you would change.
*
*/
if (!defined('ROOT')) {
  //define('ROOT', 'FULL PATH TO DIRECTORY WHERE APP DIRECTORY IS
               //LOCATED DO NOT ADD A TRAILING DIRECTORY SEPARATOR';
  //You should also use the DS define to seperate your directories
  //define('ROOT', dirname(dirname(dirname(__FILE__))));
  define('ROOT', DS . 'Users' . DS . 'myhomedir');
}
if (!defined('APP_DIR')) {
  //define('APP_DIR', 'DIRECTORY NAME OF APPLICATION';
  //define('APP_DIR', basename(dirname(dirname(__FILE__))));
  define('APP_DIR', 'blog');
}
/**
* This only needs to be changed if the cake installed libs are located
* outside of the distributed directory structure.
*/
if (!defined('CAKE_CORE_INCLUDE_PATH')) {
  //define ('CAKE_CORE_INCLUDE_PATH', FULL PATH TO DIRECTORY WHERE
        //CAKE CORE IS INSTALLED DO NOT ADD A TRAILING DIRECTORY SEPARATOR';
  //You should also use the DS define to seperate your directories
  //define('CAKE_CORE_INCLUDE_PATH', ROOT);
  define('CAKE_CORE_INCLUDE_PATH', DS . 'Library' . DS . 'Cake');
}

Now Cake knows where to find it’s library and application files.

All that’s left is to setup the database connection. Easy peasy: go to the application directory (/Users/myhomedir/blog). You’ll see a “config” folder. Within that folder, duplicate “database.php.default” and rename the copy database.php. Open up this file in your text editor of choice (I like jEdit as far as programming, by the way).

Cake is nice in that you can be simultaneously running a few databases in order to have a ‘testing’ environment and a ‘default’ environment. Since this is just a development box, I generally just modify the default array. For this setup, you need only change the “login”, “password,” and “database” values to their appropriate MySQL values. For example:

var $default = array('driver' => 'mysql',
      'connect' => 'mysql_connect',
      'host' => 'localhost',
      'login' => 'mymysqluser',
      'password' => 'foxessuckoknotreally',
      'database' => 'blog',
      'prefix' => '');

Step Five:
Start coding!

That’s all there is to it. You’re ready to code up the Blog tutorial, or start your own application. And a big plus is that for any major Cake changes, you can just install the new libraries into /Library/Cake, without having to change your application code!

Out.

This entry was posted in Code. Bookmark the permalink.