What Is New in CakePHP 4.5
CakePHP 4.5 introduces a set of focused enhancements and deprecations that prepare the framework for future major releases. This update brings quality-of-life improvements for developers while signaling changes on the horizon.
| Category | Key Changes |
|---|---|
| New Features | New Request::is() method, Cache::registerConfig(), AssetMix helper. |
| Improvements | Better CLI output formatting, PostgreSQL schema handling, and session configuration. |
| Deprecations | Several methods in ServerRequest, TableRegistry, and view-related classes. |
| Bug Fixes | Multiple fixes across the ORM, Database, Console, and View layers. |
What new HTTP utilities were added?
The ServerRequest::is() method provides a cleaner way to check the current request's HTTP method. It's a straightforward replacement for string comparisons against $request->getMethod().
// Old way
if ($request->getMethod() === 'POST') { ... }
// New in 4.5
if ($request->is('post')) { ... }
This method is case-insensitive and simplifies writing conditional logic for different request types, making your controllers and middleware more readable.
How is asset management improved?
A new AssetMix helper was introduced for seamless integration with Laravel Mix. If you use Mix for compiling frontend assets, this helper generates the correct script and stylesheet tags, including versioned filenames.
// In your template
echo $this->AssetMix->script('app.js');
echo $this->AssetMix->css('app.css');
It reads from your `mix-manifest.json` file, removing the need to manually manage cache-busting query strings. This reflects the framework's adaptation to modern PHP full-stack workflows.
What caching improvements were made?
You can now register cache configurations outside of Application::bootstrap() using the new Cache::registerConfig() method. This offers more flexibility for plugin authors and modular application design.
use Cake\Cache\Cache;
Cache::registerConfig('my_plugin', [
'className' => FileEngine::class,
'duration' => '+1 hours',
]);
In practice, this allows configuration to be loaded on-demand or from specific plugin bootstrap files, keeping your main application bootstrap file cleaner.
Which features are now deprecated?
Version 4.5 marks several methods for removal in CakePHP 5.0. Key deprecations target the ServerRequest class and the static TableRegistry.
ServerRequest Deprecations
getParam()/withParam()- UsegetAttribute()andwithAttribute()instead.getData()with a default value - Pass the default as the second argument togetData().
TableRegistry Deprecation
The static TableRegistry::getTableLocator() method is deprecated. Use dependency injection or the fetchTable() method in your Table classes.
These changes push the codebase towards a more explicit dependency injection pattern, which is a common trend in modern PHP frameworks.
Were there any console output enhancements?
Yes, the ConsoleIntegrationTestTrait received updates to capture and assert against exit codes from CLI commands. This matters because it allows for more robust testing of console applications where the exit code signals success or failure.
$this->exec('my_command --flag');
$this->assertExitCode(CommandInterface::CODE_SUCCESS);
Additionally, general improvements were made to the formatting and wrapping of multi-line output in shell classes, making complex CLI tools easier to build and maintain.
FAQ
Should I upgrade my application to CakePHP 4.5 immediately?
Yes, it's a recommended upgrade. It contains useful improvements and bug fixes. The deprecation notices will help you prepare your code for the future 5.0 release without breaking anything now.
What is the most critical deprecation to address?
Start by replacing uses of TableRegistry::getTableLocator() with dependency injection or fetchTable(). This change moves away from static calls and is a core shift for the ORM layer.
Does the new AssetMix helper require Laravel Mix?
Yes, it's specifically designed for projects that use Laravel Mix to build and version frontend assets. If you use a different build tool like Webpack Encore or Vite, you'll need a custom helper or continue using the existing HtmlHelper methods.
How does the `Request::is()` method handle custom HTTP methods?
It works with any HTTP method string. The check is case-insensitive, so $request->is('put'), $request->is('PATCH'), or $request->is('MY_CUSTOM_METHOD') will all correctly match the request's method.
Are there any database schema-related changes?
For PostgreSQL users, schema name quoting in Query::sql() was fixed. This prevents issues when your schema name contains uppercase letters or reserved keywords, ensuring generated SQL is correct.