Last week we defined the requirements and the domain model. Now, before we can start with the realization of our application we have to install and configure CakePHP. So the goal for today is to get the Cake welcome screen.

I will describe an advanced setup (i.e the core of CakePHP will be used by multiple applications) as I think it is a common setup for developers working on different projects. Such a setup involves more steps than a default setup, so if you want to do a default setup you have to skip some steps.

Get CakePHP: You can download a release of CakePHP from CakeForge, or you can get it directly via Subversion (https://svn.cakephp.org/repo/trunk resp. https://svn.cakephp.org/repo/branches/1.2.x.x for the development branch). For the application I will use the version from the development branch.

Extract archive file: For the advanced setup we only need the “cake” and “vendors” folder plus the two files “.htaccess” and “index.php”. Extract them from the archive file, it doesn’t matter where you place them. In my case, I have placed them in /home/dho/projects/cake_1.2.x.x

Bake the application skeleton: CakePHP comes with a command line tool called bake (it is available in cake/scripts) which allows you to create the application skeleton very easily. The screenshot should be self-explanatory:

Baking the application skeleton

Create the database: As database I will use MySQL (you could also use PostgreSQL, MS SQL Server, and some others, even though I don’t know how mature those drivers are). To create the database I use the command line tool which comes with MySQL as it is in my opinion the fastest way to create a database. As I will name the application “n” (short for notify), the database is also named “n”.

dho@tumulux:~$ mysql -u root
mysql> create database n;
mysql> quit
dho@tumulux:~$

Define database settings: First we have to rename app/config/database.php.default to database.php:

dho@tumulux:~/projects/n/trunk/app/config$ mv database.php.default database.php

And then we can define the database settings (you can remove the $test connection as it is not used):

class DATABASE_CONFIG
{
    var $default = array('driver' => 'mysql',
                                 'connect' => 'mysql_connect',
                                 'host' => 'localhost',
                                 'login' => 'root',
                                 'password' => '',
                                 'database' => 'n',
                                 'prefix' => '');
}

Edit app/webroot/index.php: In this step we have to specify the location of the Cake core. For this purpose we have to change the value of the constant CAKE_CORE_INCLUDE_PATH in app/webroot/index.php. In my case the statement is now:

define('CAKE_CORE_INCLUDE_PATH', '/home/dho/projects/cake_1.2.x.x');

Add hosts entry: For each project I have an entry in the form project.localhost in the file /etc/hosts. That allows me to use virtual hosts in Apache (see next step). For this application it looks like:

127.0.0.1    n.localhost    localhost

Add virtual host: The previous step is done so that I can use a virtual host for the application on my local machine. So I have to add the following to /etc/apache2/sites-available/default:

NameVirtualHost *
#
# other virtual hosts
#
<VirtualHost *>
    ServerName n.localhost:80
    DocumentRoot /home/dho/projects/n/trunk/app/webroot

    ErrorLog /var/log/apache2/n_error.log
    CustomLog /var/log/apache2/n_access.log combined
</VirtualHost>

Please note that the DocumentRoot points to app/webroot, i.e. only the webroot folder (and its subfolders) will be directly accessible with the browser, all other folders are not accessible.

After restarting Apache with

apache2 -k restart

we should now get the welcome screen when we go to http://n.localhost. And voilĂ , here it is:
Application baked

Change value of CAKE_SESSION_STRING: Since revision 4565 you will get the following notice when you go to http://n.localhost:

Notice: Please change the value of CAKE_SESSION_STRING in app/config/core.php to a salt value specific to your
application in /home/dho/projects/cake_1.2.x.x/cake/bootstrap.php on line 54

So we have to do what is stated in the notice, we have to change the value of the constant CAKE_SESSION_STRING in app/config/core.php. After a reload the notice will disappear and we will get the screen as shown above.

So, mission accomplished for today. The next mission already waits…

Update (2007-03-04): Added last step “Change value of CAKE_SESSION_STRING”