3.4. Setting Up CakePHP

The first way to setup CakePHP is generally only recommended for development environments because it is less secure. The second way is considered more secure and should be used in a production environment.

[Note] Note

NOTE: /app/tmp must be writable by the user that your web server runs as.

3.4.1. Development Setup

For development we can place the whole Cake installation directory inside the specified DocumentRoot like this:

/wwwroot
    /cake
        /app
        /cake
        /vendors
        .htaccess
        index.php

In this setup the wwwroot folder acts as the web root so your URLs will look something like this (if you're also using mod_rewrite):

www.example.com/cake/controllerName/actionName/param1/param2

3.4.2. Production Setup

In order to utilize a production setup, you will need to have the rights to change the DocumentRoot on your server. Doing so, makes the whole domain act as a single CakePHP application.

The production setup uses the following layout:

../path_to_cake_install
    /app
        /config
        /controllers
        /models
        /plugins
        /tmp
        /vendors
        /views
        /webroot <-- This should be your new DocumentRoot
        .htaccess
        index.php
    /cake
    /vendors
    .htaccess
    index.php

Example 3.1. Suggested Production httpd.conf

DocumentRoot /path_to_cake/app/webroot


In this setup the webroot directory is acting as the web root so your URLs might look like this (if you're using mod_rewrite):

http://www.example.com/controllerName/actionName/param1/param2

3.4.3. Advanced Setup: Alternative Installation Options

There are some cases where you may wish to place Cake's directories on different places on disk. This may be due to a shared host restriction, or maybe you just want a few of your apps to share the same Cake libraries.

There are three main parts to a Cake application:

  1. The core CakePHP libraries - Found in /cake

  2. Your application code (e.g. controllers, models, layouts and views) - Found in /app

  3. Your application webroot files (e.g. images, javascript and css) - Found in /app/webroot

Each of these directories can be located anywhere on your file system, with the exception of the webroot, which needs to be accessible by your web server. You can even move the webroot folder out of the app folder as long as you tell Cake where you've put it.

To configure your Cake installation, you'll need to make some changes to /app/webroot/index.php (as it is distributed in Cake). There are three constants that you'll need to edit: ROOT, APP_DIR, and CAKE_CORE_INCLUDE_PATH.

  • ROOT should be set to the path of the directory that contains your app folder.

  • APP_DIR should be set to the path of your app folder.

  • CAKE_CORE_INCLUDE_PATH should be set to the path of your Cake libraries folder.

Example 3.2. /app/webroot/index.php (partial, comments removed)

if (!defined('ROOT'))
{
    define('ROOT', dirname(dirname(dirname(__FILE__))));
}

if (!defined('APP_DIR'))
{
    define ('APP_DIR', basename(dirname(dirname(__FILE__))));
}

if (!defined('CAKE_CORE_INCLUDE_PATH'))
{
    define('CAKE_CORE_INCLUDE_PATH', ROOT);
}

An example might help illustrate this better. Imagine that I wanted to set up Cake to work with the following setup:

  • I want my Cake libraries shared with other applications, and placed in /usr/lib/cake.

  • My Cake webroot directory needs to be /var/www/mysite/.

  • My application files will be stored in /home/me/mysite.

Here's what the file setup looks like:

/home
    /me
        /mysite                  <-- Used to be /cake_install/app
            /config
            /controllers
            /models
            /plugins
            /tmp
            /vendors
            /views
            index.php
/var
    /www
        /mysite                  <-- Used to be /cake_install/app/webroot
            /css
            /files
            /img
            /js
            .htaccess
            css.php
            favicon.ico
            index.php
/usr
    /lib
        /cake                    <-- Used to be /cake_install/cake
            /cake
                /config
                /docs
                /libs
                /scripts
                app_controller.php
                app_model.php
                basics.php
                bootstrap.php
                dispatcher.php
            /vendors
 

Given this type of setup, I would need to edit my webroot index.php file (which should be at /var/www/mysite/index.php, in this example) to look like the following:

[Note] Note

It is recommended to use the 'DS' constant rather than slashes to delimit file paths. This prevents any 'missing file' errors you might get as a result of using the wrong delimiter, and it makes your code more portable.

if (!defined('ROOT'))
{
    define('ROOT', DS.'home'.DS.'me');
}

if (!defined('APP_DIR'))
{
    define ('APP_DIR', 'mysite');
}

if (!defined('CAKE_CORE_INCLUDE_PATH'))
{
    define('CAKE_CORE_INCLUDE_PATH', DS.'usr'.DS.'lib'.DS.'cake');
}