CakePHP and Composer

At LeaseWeb we are very fond of Composer, a package/dependency manager for PHP. It allows for easy software upgrades, with proper dependency management and provides an easy workflow which allows us to leave the dependencies out of the repository. This dramatically reduces the size of the repository. It also prevents programmers to fix framework issues without upstreaming the bugfixes. This is important, because patches on the framework have a high risk of complicating (security) upgrades later on.

We use Composer daily for Symfony2, which has excellent support for Composer (from version 2.1). CakePHP, another great PHP MVC framework, has recently released version 2.3.1, which is still lacking support for Composer. Fortunately it is easy to start using Composer in you CakePHP application. This guide will tell you how.

A typical CakePHP application has at least two directories at the top level: “app” and “lib”. The “app” directory contains the application, while the “lib” directory contains the CakePHP software. Composer will install CakePHP into the “vendors” directory, so we will have to change the location of CakePHP, fortunately this is very easy (see step 5, 6 and 7).

Step 0

Make a backup! So that you can revert if you are not happy with the result.

Step 1

Remove the lib directory, execute:

rm -Rf ./lib

Step 2

Get composer, execute:

curl -s https://getcomposer.org/installer | php

Step 3

Add the following composer.json file at the top level:

    {
	    "minimum-stability": "dev",
	    "config": {
	        "vendor-dir": "vendors"
	    },
	    "repositories" : [
		    {
			    "type": "package",
			    "package": {
				    "name" : "cakephp/cakephp",
				    "version" : "2.3.5",
				    "source" : {
					    "type" : "git",
					    "url" : "git://github.com/cakephp/cakephp.git",
					    "reference" : "2.3.5"
				    },
				    "bin" : ["lib/Cake/Console/cake"]
			    }
		    }
	    ],
	    "extra": {
		    "installer-paths": {
			    "app/Plugin/DebugKit": ["cakephp/debug_kit"]
		    }
	    },
	    "require" : {
		    "php": ">=5.2",
		    "cakephp/cakephp" : "2.3.5",
		    "cakephp/debug_kit": "2.2.*"
	    }
    }

Step 4

Install CakePHP and all its dependencies:

php composer.phar update

Step 5

If you have properly pointed your Apache webroot to “/app/webroot” you have to search this line in “app/webroot/index.php” and “app/webroot/test.php”:

define('CAKE_CORE_INCLUDE_PATH', ROOT . DS . 'lib');

And change it into:

define('CAKE_CORE_INCLUDE_PATH', ROOT . DS . 'vendors'.DS.'cakephp'.DS.'cakephp'.DS.'lib');

Step 6

If you use the root directory of the project as your Apache DocumentRoot (not recommended) you should also search and replace the same line in the top level “index.php”

Step 7

To repair the Cake Console you need to change to following line in “app/Console/cake.php”:

$root = dirname(dirname(dirname(__FILE__)));

And change it into:

$root = dirname(dirname(dirname(__FILE__))).$ds.'vendors'.$ds.'cakephp'.$ds.'cakephp';

Step 8 (optional)

As Miles Johnson writes on his blog how you can use Composer’s autoloader. All you need to do is add the following line to top of “app/Config/bootstrap.php”:

require_once dirname(dirname(__DIR__)) . '/vendors/autoload.php';
Share