Installing CakePHP with Composer

I really like how composer simplifies dependency management & installation. It can make applications more portable, and simpler to deploy when compared to the pear installer. Another really nice feature of composer is that you can easily install any pear package, making it perfect for installing CakePHP. While installation was simple, some extra work was required to get things up and running smoothly.

Setup the composer file

In order to use composer, we’ll need a composer.json file. The composer.json in my side project looked like:

Show Plain Text
  1. {
  2.     "name": "status-site",
  3.     "repositories": [
  4.         {
  5.             "type": "pear",
  6.             "url": ""
  7.         }
  8.     ],
  9.     "require": {
  10.         "pear-cakephp/cakephp": "2.3.3"
  11.     },
  12.     "config": {
  13.         "vendor-dir": "Vendor/"
  14.     }
  15. }

After getting the composer.json file setup, you can use php composer.phar install to download and install CakePHP.


This constant is important for CakePHP to find itself and know where in the filesystem it lives. Thankfully the directory structure that composer uses is very consistent. In the app/webroot/index.php I defined CAKE_CORE_INCLUDE_PATH to be:

Show Plain Text
  1. define('CAKE_CORE_INCLUDE_PATH',  ROOT . DS . APP_DIR . '/Vendor/');

This ensures the various constants that CakePHP relies on are defined properly. The cake console command can be run using Vendor/bin/cake as of CakePHP 2.3.3.

Autoloading troubles

Normally when using composer you simply require the Vendor/autoload.php. Composer configures its autoloader to be the first autoloader in the chain. This works great until your application uses a classname that also exists anywhere within CakePHP, even in the test cases. Unfortunately the CakePHP test cases include a User class so the chances of you seeing a “Fatal Error: Cannot redeclare AppModel” are pretty high. To fix this I had to re-register the CakePHP autoloader so it was called before falling back to composer. In my Config/bootstrap.php I added the following:

Show Plain Text
  1. // Load composer autoload.
  2. require APP . '/Vendor/autoload.php';
  4. // Remove and re-prepend CakePHP's autoloader as composer thinks it is the most important.
  5. // See
  6. spl_autoload_register(array('App', 'load'), true, true);

After this change things worked perfectly. Both classes autoloaded via App and vendor classes loaded by composer work. While using composer to install CakePHP requires a bit more initial extra setup. I think the benefit of having per application CakePHP libraries, and being able to leverage the rest of the composer eco-system are well worth the effort.


Composer is great, but for me still hard to configure. This is nice post to explain how to set up CakePHP itself, but what about plugins, for example your AssetComporess plugin. How would you write composer.json to get CakePHP and AssetComporess as well… and configuration? Having AssetCompress in Vendor folder instead of Plugin folder seems to be problem.

Luboš on 4/25/13

Luboš AssetCompress should work similarly to DebugKit. See here

The composer installer actually knows where to put CakePHP plugins ;)

Heath on 4/25/13

Hi Mark, thanks for taking the time to post this. I receive an ‘Invalid PEAR package. package.xml defines file that is not located inside the tarball.’ error. I got it to work by specifying ’2.3.2’ rather than ’2.3.3’; it seems there’s something awry with the 2.3.3 package. I’ll create a LightHouse ticket, but I thought I’d post this here for others.

Tim on 4/27/13

Tim: Things should be good now. I re-built the PEAR package. You may need to clear your composer cache directory to force the new package to be downloaded.

mark story on 4/27/13

Hi and thanks for this clear and simple how-to.

I suppose that I should also update my .gitignore file to add “app/Vendor/” and “app/Plugin/” to the ignore list ?

Julien on 4/30/13

This is awesome, however there’s one additional issue which I just spent the last half hour debugging.. The problem lies with the `EmailConfig` class, which is not handled by `App::load()` but rather manually loaded. Before it’s loaded though, `CakeEmail` checks whether `class_exists(‘EmailConfig’)` is true; which causes the Composer autoloader to start looking for it.

At this point it’s basically a gamble which one it’s going to load: the one in `Config/email.php` or the one in `CakeEmailTest.php` in the core’s test suite. On my PC it loads the former – so no problem. On my colleague’s PC it loads the latter though, leaving him with a vague “Class PHPUnit_Framework_TestCase could not be found” fatal error.

My solution at this point is to manually include `email.php` in bootstrap, as to prevent the autoloader from being fired at all.

Elte Hupkes on 6/4/13

Elte: That is a bit of a pain, however your solution seems pretty reasonable to me.

mark story on 6/16/13

Mark, Elte,

By manually including `email.php` in bootstrap, the core `Network/Email/CakeEmail` test fails when trying to declare the `EmailConfig` class a second time.

The solutions I was able to find were:

- Symlink `app/config/EmailConfig.php` to `app/Config/email.php` and adding the `EmailConfig` class to `App::$paths` by adding `App::uses(‘EmailConfig’, ‘Config’)` to bootstrap.

- Rename the `email.php` file to `EmailConfig.php` and add it to `App::$paths` the same way as the previous solution.

I opted for the symlink solution in order to make sure it doesn’t break any calls to `config(‘email’)` in other places than in `CakeEmail.php`.

In both cases, the `EmailConfig` class only gets declared once when running the `CakeEmail` test.

Jad Bitar on 7/12/13

Sorry, forgot to also mention that I needed to also re-define ‘CAKE_CORE_INCLUDE_PATH’ in `app/webroot/test.php` just like in `app/webroot/index.php`.

Jad Bitar on 7/12/13

i will be back for more articles like this :)

Zach Smith on 7/18/13

To run tests from the command line, I also had to change /Console/cake.php, lines 24 / 25 to this:

$root = dirname(dirname(FILE));
ini_set(‘include_path’, $root . $ds . ‘Vendor’ . $ds . ‘’ . $ds . ‘CakePHP’ . PATH_SEPARATOR . ini_get(‘include_path’));

Joshua Paling on 9/23/13

Even when I reregisterd the Cake Autoloader, I got Autoloading conflicts when using the auth Component, because of the Models Group, User and Product are defined a second time in Cake/Test/Case/Model/models.php

I resolved the issue by using a composer hook script and deleting the mapping within composers autoload_classmap

“scripts”: { “post-autoload-dump”: [ “./” ] },
…. #!/bin/bash
mv vendors/composer/autoload_classmap.php vendors/composer/autoload_classmap.php.bak
sed ‘/CakePHP\/Cake\/Test\/Case\/Model\/models\.php/d’ vendors/composer/autoload_classmap.php.ori > vendors/composer/autoload_classmap.php

I hope it helpes somebody else, too

Marco on 1/6/14

Does it make sense to remove the whole cake tree out of the composer autoloader, because we still use the cake autoloader for these classes?

sed ‘/CakePHP\/Cake/d’ vendors/composer/autoload_classmap.php.ori > vendors/composer/autoload_classmap.php

Marco on 1/6/14

Hi, i’m having an issue when i’m trying to run:
php composer.phar install

Problem 1 – The requested package pear-cakephp/cakephp could not be found in any versi
on, there may be a typo in the package name.

Potential causes: – A typo in the package name – The package is not available in a stable-enough version according to your min
imum-stability setting see f
or more details.

cristina on 8/14/14


“require”: { “cakephp/CakePHP”: “2.3.3” }

Nadir Latif on 12/20/14

They just aren’t educated enouvh too know how or where to look for their niche.
A few of the most common “reasons“I have encountered.
My so-called portable computer was a lighter-weight desktop
Mac and my days, and nights, were speent wearing the many hats formerly worn byy the many employees that had previously supported
me in corporate. These are just some of the options available for those who want to become Internet entrepreneurs.

peluang usaha 2015 on 1/30/15

?n voit de suite qu? vous maîtrisez bien c? ?ue vou? dites

XXX hardcore gratuit on 1/30/15

And all this really is carried out by indicates of the state-of-the-art
multi-touch display. Nokia’s executive vice president
of pull, said a huge consumer base is an important advantage of Nokia.
2 version in ruining well, so users can easily use their
cell phone as Wi-Fi hotspot.

looney tunes dash cheat on 2/1/15

The screen is a touch screen and is a hair larger than three inches in size.
They scores well in terms of features, apps, looks, design, efficiency, functionality, performance and quality.
After it’s installed, sign in with your Gmail account details,
the same you used to create the Google Voice account.

sniper 3d assasin hack on 2/1/15

We structure our programs to drastically minimize the risk that is inevitable
with all debt elimination programs. There are many ways
in which you can learn things but a platform is needed to prove your skills.
Mc – Nair spoke to Bentley on a variety of
issues, but it was his comments on expanding the regular season, and
in particular his perspecive on pre-season pay that caught
my attention.

hack simcity buidit on 2/1/15

Comments are not open at this time.