Installing CakePHP 2 Using Composer
So I was trying to update and refactor a legacy code that I was working on which used CakePHP 2. I also have to update the CakePHP version since it was really outdated.
The first approach I took was to just download the CakePHP repository, and copy the lib
directory, and overwrite the project’s lib
directory. But this wasn’t good enough. So I was wondering if it is possible to just use composer.
I ended up looking at the Advanced Installation guide from the official docs. But I ended up making several mistakes.
In this guide, I am going to show you how I generated a new CakePHP 2 app using composer.
Pre-requisites
Before we get started, you have to make sure that you have the following installed in your machine:
- PHP 5.3 or higher
- Composer
Note: In this guide, I am going to assume that you have Composer installed Globally.
Getting Started
First of all, we have to create the project directory:
mkdir cakephp-app && cd cakephp-app
Now the command below will create a composer.json file with cakephp as dependency. The -–no-update will prevent it from installing the dependencies to the default directory:
composer require --no-update cakephp/cakephp:2.10.*
Set dependency directory to Vendor:
composer config vendor-dir Vendor/
And install the dependencies
composer install
Generate the application skeleton:
Vendor/bin/cake bake project . --empty
Welcome to CakePHP v2.10.17 Console --------------------------------------------------------------- App : cakephp-app Path: /Users/onoblog/codes/cakephp-app/ --------------------------------------------------------------- Skel Directory: /Users/onoblog/codes/cakephp-app/Vendor/cakephp/cakephp/lib/Cake/Console/Templates/skel Will be copied to: /Users/onoblog/codes/cakephp-app/. --------------------------------------------------------------- Look okay? (y/n/q)
And just enter y when prompted.
--------------------------------------------------------------- Created: app in /Users/onoblog/codes/cakephp-app/. --------------------------------------------------------------- * Random hash key created for 'Security.salt' * Random seed created for 'Security.cipherSeed' * Cache prefix set * app/Console/cake.php path set. CakePHP is not on your `include_path`, CAKE_CORE_INCLUDE_PATH will be hard coded. You can fix this by adding CakePHP to your `include_path`. * CAKE_CORE_INCLUDE_PATH set to /Users/onoblog/codes/cakephp-app/Vendor/cakephp/cakephp/lib in webroot/index.php * CAKE_CORE_INCLUDE_PATH set to /Users/onoblog/codes/cakephp-app/Vendor/cakephp/cakephp/lib in webroot/test.php * Remember to check these values after moving to production server Project baked successfully!
Don’t forget to create the database config file!
cp Config/database.php.default Config/database.php
And that’s it! Now you just have to update the database configuration with the valid database credentials.
And also, if you are using some version manager like Git, remember to ignore Vendor and tmp directory.
By using composer, it is much easier to update the CakePHP version.
Make sure to check out my post: Setup CakePHP 2.x Application with Docker from Scratch, and try to startup your newly created CakePHP application using docker.