Saturday 17 May 2008

CakePHP 1.2 .vs. 1.1 - Changes #2

Another minor difference to CakePHP in v1.2: template file extensions have changed.

In v1.1, page templates had a filename extension thtml. In v1.2 it is ctp.

The IBM Wiki tutorial suggests that you play with the page template app/views/layouts/default.thtml at the end of part 1. Using CakePHP v1.2, this is app/views/layouts/default.ctp.

CakePHP 1.2 .vs. 1.1 - Changes #1

Being relatively new to CakePHP, I have been working through the IBM tutorials. In particular, the Wiki tutorial fits closely to what I want to build for my first CakePHP implementation.

Part 1 of the tutorial was straightforward. This covers the basics of creating a simple set of models, views and controllers. The result is a set of web pages which display data in tables and offer add / edit / delete functionality.

The tutorial is written for v1.1 of the framework. I have opted for v1.2 - so there are some differences.

The first difference is in the scaffolding. CakePHP v1.1 provides a PHP script "bake.php" in the cake/scripts directory. This is an interactive process and you've got to respond to the menus to drive the scaffolding.


$ php ../scripts/bake.php

Baking ...
----------------------------------------------------------
Name: app
Path: ~/dev/workspace/cakephp/app
----------------------------------------------------------
[M]odel
[V]iew
[C]ontroller

What would you like to bake? (M/V/C)
>


CakePHP v1.2 has a command script 'cake' in the cake/console directory. This can be given command-line args so that the scaffolding can be completed quickly with a few simple commands:


$ ../console/cake bake all users


This then builds a model, view and controller for the named table in the database.

Tuesday 22 April 2008

Installing CakePHP into a Subdirectory

CakePHP is designed to be installed into a webroot directory. The .htaccess files are pre-configured to make everything work out of the box ...

... except in my setup. I have installed CakePHP into a subdirectory of my development root. This seems to confound CakePHP - which is possibly due to some Apache settings.

I have my development in a subdirectory of my home:
/home/raymond/dev/workspace
. My Apache configuration sets this as the root for http://localhost/dev/:
Alias /dev "/home/raymond/dev/workspace
<Directory "/home/raymond/dev/workspace">
AllowOverride All
Order allow,deny
Allow from all
</Directory>


I use Eclipse, so this seems to make sense. This means I can have
/home/raymond/dev/workspace/birthclass
for the birthclass site development, and now:
/home/raymond/dev/workspace/cakephp
for the CakePHP experiment. All fairly logical so far ...

... when having followed a tutorial, all I get from Apache is:
The requested URL /home/raymond/dev/workspace/cakephp/app/webroot/ was not found on this server.

After some monkeying around, I have found the trick: edit the .htaccess files. There are three: in the CakePHP root, app and app/webroot. In each case, the RewriteBase must be defined. In my case, these are /dev/cakephp/, /dev/cakephp/app/ and /dev/cakephp/app/webroot/ respectively. So now, my cakephp/app/webroot/.htaccess file look like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /dev/cakephp/app/webroot/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

And now I have a fully functioning CakePHP setup in its own subdirectory. When I request http://localhost/dev/cakephp, I get my CakePHP application. When I request http://localhost/birthclass, I get my birthclass application.

Job done. On to the tutorials ...

Starting From the Beginning

This blog will document my explorations into web application development using CakePHP.

My current hosting provides MySQL and PHP on a Linux platform. I can't justify the expense of a server all to myself - even a virtual one. I don't want to have to make sure my server at home is up 27*7 .. so PHP is my language.

I'm fine with this: I have some experience with programming in PHP and have dabbled with Mambo (now Joomla!), Exponent CMS and Drupal. PHP is fairly powerful when it gets going, but is simple enough to knock up code quickly.

Whilst sniffing around the web - reading up on Ruby on Rails as it happens - I stumbled upon CakePHP. I've just completed a custom CMS to back the website I've been maintaining, so this seems an ideal time to see how CakePHP could have made the framework creation simpler.

So ... watch this space.

Oh, and do feel free to correct me and make suggestions!