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 | 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://",
					    "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';

6 thoughts on “CakePHP and Composer”

  1. To fix the Cake console after moving the CakePHP core directory you should patch app/Console/cake.php with the following diff:

        --- a/app/Console/cake.php
        +++ b/app/Console/cake.php
        @@ -28,7 +28,7 @@
            // /lib/Cake/Console/Templates/skel/Console/cake.php
            ini_set('include_path', $root . $ds . 'lib' . PATH_SEPARATOR . ini_get('include_path'));
        +ini_set('include_path', $root . $ds . 'vendors' . $ds . 'cakephp' . $ds . 'cakephp' . $ds . 'lib' . PATH_SEPARATOR . ini_get('include_path'));
         if (!include($dispatcher)) {
            trigger_error('Could not locate CakePHP core files.', E_USER_ERROR);
  2. @Mark: I had indeed overlooked “app/Console/cake.php” (and also “app/webroot/test.php”). I updated the post. Thank you!

  3. Hi,

    I followed your instructions but I think something went wrong. Maybe you can help me.

    The problem I have is that CakePHP got installed inside vendors/cakephp/cakephp when I followed your composer.json file and ran “php composer.phar update” command. The root app folder contains the Plugin folder inside which DebugKit got installed.

    Now my question is, Is that how it’s supposed to be? Should all the files go inside vendors or did something go wrong? If the folder structure is correct, does it mean that all my website files should go inside vendors/cakephp/cakephp/app?

    I’m using composer for the first time and this is very confusing to me. I would really appreciate some assistance/guidance.

  4. @JB: Yes, that how it’s supposed to be. And to answer to your second question: No, all your website files should go inside /app, not vendors/cakephp/cakephp/app.

  5. @John: Thank you. I did not now about that plugin.

Leave a Reply

Your email address will not be published. Required fields are marked *