As you know, CakePHP announced the version 4.x last December.I recommend that you consider upgrading your applications to the next version, to keep up to date and get all the benefits. Now, let's see how to bake!
Step 1: Upgrade PHP
First things first, if you are not running on PHP 7.2 or higher, you will need to upgrade PHP before updating CakePHP. CakePHP 4.0 requires a minimum of PHP 7.2.
Step 2: Upgrade Templates and Resources
There is an upgrade CLI tool for rename and moving the templates and resources:
Templates and Resources must have been moved and renamed, check the result below:
* This project doesn't have Resources files
Now, let's create a new constant for Resources on /config/paths.php:
Finally, update the paths on config/app.php:
Step 3: Upgrade CakePHP
The next step is optional (and the Migration Guide included this) - run the rector command to automatically fix many deprecated method calls:
The rector applied on codebase some return type declarations:
Pay attention: It is important to apply rector before you upgrade your dependencies.
Upgrade CakePHP and PHPUnit:
PHPUnit can be upgraded easily. Most of the time, the --update-with-dependencies doesn’t work with me for CakePHP:
The root of the issue is the packages using Caret Version Range, so let’s update debug_kit, migrations and bake using editor:
Here we go:
Now, let see how the project looks:
Here, we have few deprecations and warnings. Do you remember I mentioned the rector is optional? So, the question is the rector and it's not always able to handle these issues.
I will use the PHPStan to fix this - we will install with composer:
Now, we can run the phpstan analyse and fix the issues:
It's up to you how much effort you will put in with PHPStan issues. I recommend fixing everything. For this post, I did fix only what was needed to run the project after the update, you can check the fixes on this commit.
After the last fixes, the project is running well:
That’s all? No. But we upgraded CakePHP? Yes.
Real applications probably use many plugins, and if these plugins don't have a version for CakePHP 4, you will need to update. Depending on the size and level of complexity of the project, the upgrade could be hard, but never impossible.
If you do not feel confident or your company would like to outsource support for this, don't hesitate to contact us at Cake Development Corporation.
Our team is offering a full upgrade from CakePHP 2/3 to CakePHP 4. This will be a migration of your current application code to make it compatible with CakePHP 4 features, plugins, security settings, etc. We will be doing these migration services for a special rate - something we have never done before! Learn more about our Upgrade Services
You can check the codebase of the examples on this repository. The branch upgrade has all steps by commit.
With every release CakePHP gets better, and version 4.x is no exception. There are many benefits that come with upgrading, and it makes baking a lot easier.