Skip to main content

skip to main content

developerWorks  >  Open source  >

Cook up Web sites fast with CakePHP, Part 1: Getting started

Quick and easy PHP rapid-development aid

developerWorks
Go to the previous pagePage 2 of 11 Go to the next page

Document options
PDF format - Fits A4 and Letter

PDF - Fits A4 and Letter
645 KB (35 pages)

Get Adobe® Reader®

Sample code


My developerWorks needs you!

Connect to your technical community


Rate this tutorial

Help us improve this content


Installation

CakePHP wants to make your life easier, regardless of your level of experience, by making your applications easier to maintain and quicker to write. CakePHP is full of cool and useful features. CakePHP wants to handle your Ajax, your data validation, your sessions. It will even slice your bread if you can write a plug-into tell it how. But you can't use CakePHP yet. You need to install it first.

Unpack and install

For the purpose of this tutorial, the entire CakePHP installation directory should be unpacked within the webroot of your Web server. In Listing 1, the webroot is /webroot.


Listing 1. Unpacking the CakePHP installation directory
                
unzip cake_1.2.2.8120.zip
cd cake_1.2.2.8120
mv * /webroot
                

Type ls -la /webroot to list the contents of the webroot and verify that the files have been moved properly. The output should look something like Listing 2.


Listing 2. Output of the ls command
                    
-rw-r--r--    1 YOURUSER  YOURGROUP       139 2007-12-15 22:50 htaccess
drwxr-xr-x   12 YOURUSER  YOURGROUP      4096 2007-12-15 22:50 app
drwxr-xr-x    7 YOURUSER  YOURGROUP      4096 2007-12-15 22:50 cake
-rw-r--r--    1 YOURUSER  YOURGROUP      2303 2007-12-15 22:50 index.php
drwxr-xr-x    5 YOURUSER  YOURGROUP      4096 2007-12-15 22:50 vendors

The directory app/tmp needs to be writable by your Web server. Confirm the permissions on this folder by typing ls -l app. The output will probably look similar to Listing 3.


Listing 3. Confirming the folder permissions
                
-rw-r--r--    1 YOURUSER  YOURGROUP       139 2007-12-15 22:50 .htaccess
drwxr-xr-x    3 YOURUSER  YOURGROUP      4096 2007-12-15 22:50 config
drwxr-xr-x    3 YOURUSER  YOURGROUP      4096 2007-12-15 22:50 controllers
-rw-r--r--    1 YOURUSER  YOURGROUP       953 2007-12-15 22:50 index.php
drwxr-xr-x    3 YOURUSER  YOURGROUP      4096 2007-12-15 22:50 locale
drwxr-xr-x    3 YOURUSER  YOURGROUP      4096 2007-12-15 22:50 models
drwxr-xr-x    2 YOURUSER  YOURGROUP      4096 2007-12-15 22:50 plugins
drwxr-xr-x    5 YOURUSER  YOURGROUP      4096 2007-12-15 22:50 tests
drwxr-xr-x    6 YOURUSER  YOURGROUP      4096 2007-12-15 22:50 tmp
drwxr-xr-x    3 YOURUSER  YOURGROUP      4096 2007-12-15 22:50 vendors
drwxr-xr-x    8 YOURUSER  YOURGROUP      4096 2007-12-15 22:50 views
drwxr-xr-x    6 YOURUSER  YOURGROUP      4096 2007-12-15 22:50 webroot
                

The simplest way to accomplish this is probably the most common and least secure: give write permissions to everyone.

chmod -R 777 app/tmp
ls -l app
                

The permissions for the tmp folder should have been updated, as shown below.

drwxrwxrwx    7 YOURUSER  YOURGROUP      2007-12-15 22:50 tmp
                

Giving write permissions to everyone is not recommended for general use. Ideally, you should change the ownership of this folder to match the user that the Web server uses, or add the user that the Web server uses to a the group for the directory and add group write permissions. This tutorial is intended to demonstrate how to use CakePHP and is not designed to be a guide for building secure applications. While security should be at the head of any application development, a full discussion of secure PHP practices is outside the scope of this tutorial.

For a production installation, change the webroot of the Web server to app/webroot, which will minimize the amount of code accessible via the Web browser and help enhance the security of your installation.



Back to top


Validation 1

In a browser, go to the URL that corresponds with the webroot for your Web server. For example, if you've installed CakePHP into the webroot of your localhost, go to http://localhost; you should see the CakePHP default home page.


Figure 1. The CakePHP default home page as it should be seen
The CakePHP default home page as it should be seen

Note: If the default home page looks more like Figure 2, then mod_rewrite is not working the way CakePHP requires. This can sometimes be a problem for first-time users.


Figure 2. The incorrect-looking home page
The incorrect-looking home page

Following are some things to verify.

Is your .htaccess file correct?

You should have gotten an .htaccess file in the CakePHP installation directory. On most *nix systems, this will be hidden from view by default. If you do not have the file, check the source you downloaded or get a fresh update from CakePHP.org. Confirm that the file exists and is valid by going to the installation directory and running cat .htaccess. This will display the file's contents, which should look like Listing 4.


Listing 4. Confirming that the .htaccess file exists
                 
<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule    ^$ app/webroot/    [L]
   RewriteRule    (.*) app/webroot/$1 [L]
</IfModule>
                

Is mod_rewrite enabled for the server?

Make sure that mod_rewrite is enabled for your Web server. For Apache, there are two lines that should appear in the httpd.conf file. In the LoadModule list, you should see the following line (or something very close to it): LoadModule rewrite_module libexec/mod_rewrite.so. In the AddModule list, you should see this line (or something very close): AddModule mod_rewrite.c.

If you cannot find these lines in your httpd.conf file, mod_rewrite is not enabled. Consult your server documentation for details on how to do this.

Does the server allow .htaccess override?

Make sure your Web server is configured to allow .htaccess override. For Apache, each directory should be defined in the httpd.conf file. These definitions can look very different from installation to installation, but you should still see the line AllowOverride All in the definition. Your definition might look something like Listing 5.


Listing 5. Definitions in the httpd.conf file
                    
   <Directory "/webroot">
       Options Indexes MultiViews
       AllowOverride All
       Order allow,deny
       Allow from all
   </Directory>
                

Consult your server documentation for more details about .htaccess override.



Back to top


Configuring a database connection

Now that you've got CakePHP installed and on speaking terms with your Web server, you need to introduce CakePHP to your database. This section will cover setting up the database configuration and verifying that CakePHP likes your database. Tor is going to need a place to store its user and product data. You'll be soon be creating a users table to be used to build the login and registration parts of Tor.



Back to top


Editing the database configuration file

Setting up your database configuration is pretty darn easy, but before you start, make sure your database server is running, that you have created a database for your application, and that you have a username and password for a user with rights to the database.

To start, make a copy of the app/config/database.php.default file and save it as app/config/database.php. Do this to preserve a copy of the original template. Open the file in your favorite text editor and look for the following section (it should be just near the bottom of the file).


Listing 6. The app/config/database.php.default file
                    
var $default = array(
                'driver' => 'mysql',
                'persistent' => false,
                'host' => 'localhost',
                'login' => 'user',
                'password' => 'password',
                'database' => 'database_name',
                'prefix' => ''
        );
                

Modify this information to fit your installation:

driver
This can be mysql, mysqli, postgres, sqlite, mssql, db2, oracle, adodb or pear-drivername. This tutorial assumes mysql.
persistent
This field tells CakePHP whether it should use persistent database connections. This should be true or false. This tutorial assumes false.
host
This is the hostname of your database server, such as localhost or mysql.yourdomain.
login
This is the username for your database login, such as dbuser.
password
This is the password for your database login, such as secretsecret.
database
This is the name of the database you wish to use, such as cakedev.
prefix
Prefix is a string, such as cp_, that is prepended to table names for any database call made by CakePHP. Using a prefix may be necessary if the database is shared among applications to keep tables from stepping on each other where two or more applications want a table with the same name, such as users.

Don't forget to save the file.

Any number of database configurations can be specified in database.php provided they have distinct names. You can specify which database configuration the application should use in the models.

Some notes about databases and CakePHP:

  • The tables must have a primary key named id.
  • If you include a created or modified column in the table, CakePHP will automatically populate the field when appropriate.
  • Table names should be plural (users, products, eggs, sodas, winners, losers). Their corresponding models will have singular names (user, product, egg, soda, winner, loser).
  • If tables are to be related, foreign keys should follow the format table_id with singular table names. For example, user_id, product_id, egg_id, soda_id, winner_id and loser_id would be foreign keys for the table's user, product, egg, soda, winner, and loser.


Back to top


Validation 2

Go back to the URL you used to validate the initial installation. You should see that the CakePHP default home page has changed to indicate the status of your database configuration.


Figure 3. CakePHP default home page has changed to indicate status of database configuration
CakePHP default home page has changed to indicate status of database configuration

If the default home page says the database configuration file is not present, you may have put it in the wrong place or misnamed it. Make sure the database configuration file is app/config/database.php. If the default home page says CakePHP is unable to connect to the database, confirm that the connection information you entered is valid and retry.



Back to top


Creating the application tables

So now CakePHP, your Web server, and your database are all on speaking terms. Time to roll up your sleeves and get to the real work. Tor needs a users table.

This table will contain the basic information necessary to identify and interact with a user. A simple username and password field would probably suffice, but other information can be useful, such as an e-mail address (for sending password reset requests), first and last name (for personalization), and last login date (to help track inactive accounts). You probably want your username and e-mail fields to be unique. And don't forget the primary key ID field. The SQL to create your table might look something like Listing 7.


Listing 7. SQL to create your table
                    
CREATE TABLE 'users' (
'id' INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
'username' VARCHAR( 40 ) NOT NULL ,
'password' VARCHAR( 40 ) NOT NULL ,
'email' VARCHAR( 255 ) NOT NULL ,
'first_name' VARCHAR( 40 ) NOT NULL ,
'last_name' VARCHAR( 40 ) NOT NULL ,
    UNIQUE (
        'username' ,,
             'email'
		)
) TYPE = MYISAM ;
                

Notice that the username, password, first_name, and last_name fields have a maximum character length of 40. We will enforce this character length in the user model. The 40-character maximum length, in this case, is entirely arbitrary.



Back to top



Go to the previous pagePage 2 of 11 Go to the next page