Update: I have created a command line tool that provides the functionality explained in detail below. Check-out CakeCore and learn more.

How to maintain one copy of each major version of the CakePHP Core library. Making upgrades to new versions of the framework effortless even across many applications.

I use CakePHP for the majority of web-based applications I create. Before developing this convention I would end up with many redundant copies of the Core library floating around taking up disk space, and making upgrade tedious.

To overcome this dilemma, CakePHP allows you to store the core library outside the project and point to its new location by uncommenting the constant CAKE_CORE_INCLUDE_PATH in app/webroot/index.php. This means you can have one copy of the core library for any number of CakePHP applications.

CakePHP can be installed using your distributions package management tool apt-get or yum. The problem with this approach is the version is always several releases behind the current stable build. To overcome this annoyance, I use the following convention to maintain multiple version of the CakePHP core library on all of my servers.

Install and Update the CakePHP Core Library

The logical place for CakePHP to live on my systems is /usr/lib/cakephp, along side Pear and other libraries. I also maintain multiple versions of the framework as I have 1.3 projects and many 2.x projects.

Create the following directory structure to separate each version.

/usr/lib/cakephp/
/usr/lib/cakephp/1.2/
/usr/lib/cakephp/1.3/
/usr/lib/cakephp/2.0/
/usr/lib/cakephp/2.1/

Use git to install and checkout each version of the framework

cd /usr/lib/cakephp/1.3
sudo git clone https://github.com/cakephp/cakephp.git /usr/lib/cakephp/1.3
sudo git checkout 1.3

cd /usr/lib/cakephp/2.0
sudo git clone https://github.com/cakephp/cakephp.git /usr/lib/cakephp/2.0
sudo git checkout 2.0

cd /usr/lib/cakephp/2.1
sudo git clone https://github.com/cakephp/cakephp.git /usr/lib/cakephp/2.1
sudo git checkout 2.1

When the CakePHP team releases new builds update each version using git

cd /usr/lib/cakephp/1.3
sudo git pull

cd /usr/lib/cakephp/2.0
sudo git pull

cd /usr/lib/cakephp/2.1
sudo git pull

cd /usr/lib/cakephp/2.1
sudo git pull

Configure each of your projects to use the Core library

For CakePHP 2.1 applications, edit app/webroot/index.php, uncomment and change line 59. Once tested, you can delete the lib/Cake/ directory from your project.

define('CAKE_CORE_INCLUDE_PATH', DS . 'usr' . DS . 'lib' . DS . 'cakephp' . DS . '2.1'  . DS . 'lib');

For CakePHP 1.3 applications, edit app/webroot/index.php and change the following around line 53. Once tested, you can delete the cake/ directory from your project.

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

Install and maintain CakePHP into the same location (/usr/lib/cakephp) on all development, testing, and production machines. Your application will always just work and you will also be storing much less in your code repos!

Update: I have created a command line tool that provides the functionality explained above. Check-out CakeCore and learn more.

Keywords:

  • cakephp multiple apps