CakePHP: One core, many apps

CakePHP allows you to use one set of core files while maintaining multiple applications. It only takes a few steps, but may not be completely straightforward the first time you try. In my example, I have my workspace in ~/dev. I would setup my project in ~/dev/client_name/project_name. I place my cake core files in separate folders for each version in ~/dev/lib/cakephp.
The basics

  1. Download and install the latest version of CakePHP in a good location.Examples:
    • Linux: /usr/lib/cakephp/cake_1.2.2.8120
    • Windows: C:\lib\cakephp\cake_1.2.2.8120
  2. Copy the contents of the app folder to your project root. This is typically your webserver root or a folder within.
  3. Edit the following constants in webroot/index.php:
    1. ROOTYou’ll want to change it so the root is one level back (your project root)

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

    2. APP_DIRThis one just gets set to blank since your root is also your app folder

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

    3. CAKE_CORE_INCLUDE_PATHSet the path to your cake core files. This can either be relative (../../core) or absolute (/path/to/core)

      if (!defined('CAKE_CORE_INCLUDE_PATH')) {
      define('CAKE_CORE_INCLUDE_PATH', '..'.DS.'..'.DS.'..'.DS.'lib'.DS.'cakephp'.DS.'cake_1.2.2.8120');
      }

      or

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

It may take a little work to get the paths right, but it is well worth it. If you try to access your project and you get PHP errors, it probably means something is wrong with the core path above. The next project you start, just copy the app folder again as above, and copy the same index.php into any new projects you start.
Different paths for dev and live
The above example only covers one server configuration. Some developers may be developing in Windows while their production server runs Linux. In that case you will need to account for each server. A simple if/else statement will take care of this. In my case, I have “localdev” as the hostname for my local development server. Here is what my CAKE_CORE_INCLUDE_PATH configuration looks like:

if (!defined('CAKE_CORE_INCLUDE_PATH')) {
if ($_SERVER['SERVER_NAME'] == 'localdev') {
define('CAKE_CORE_INCLUDE_PATH', '..'.DS.'..'.DS.'..'.DS.'lib'.DS.'cakephp'.DS.'cake_1.2.2.8120');
} else {
define('CAKE_CORE_INCLUDE_PATH', DS.'usr'.DS.'lib'.DS.'cakephp'.DS.'cake_1.2.2.8120');
}
}

Upgrading CakePHP
Upgrading CakePHP is pretty easy. Just install the latest version of CakePHP as described above. Then when you are ready to upgrade your app, change the core path again and start testing. If you run into issues with the latest version, it is really easy to switch back.