What Is New in CakePHP 3.8
CakePHP 3.8 introduces several key updates focused on modernizing the request/response layer and refining core components. This release sets the stage for future improvements while maintaining strong backwards compatibility.
| Category | Key Changes |
|---|---|
| New Features | PSR-15 Middleware support, New Application class, New Command interface. |
| Improvements | Refined Cache and Log engines, Updated Table::findOrCreate(), Better typehints. |
| Deprecated | Legacy Dispatcher, Controller::$modelClass, Network\CurlRequest. |
| Bug Fixes | Various fixes in ORM, Database, and View layers. |
How does middleware work in CakePHP 3.8?
CakePHP 3.8 adds experimental support for PSR-15 middleware, a significant shift towards standard HTTP request handling. This is implemented through a new Cake\Http\MiddlewareQueue and a revamped Application class that replaces the traditional dispatcher setup.
In practice, your src/Application.php now defines a middleware method where you can queue middleware. This approach is more flexible and interoperable with components from other frameworks.
// In src/Application.php
public function middleware($middlewareQueue)
{
$middlewareQueue
->add(new ErrorHandlerMiddleware(Configure::read('Error')))
->add(new AssetMiddleware());
return $middlewareQueue;
}
What changed in the Application bootstrap?
The bootstrap process is now centered around the Application class. The old Dispatcher is deprecated. Your webroot/index.php file now creates an instance of your Application and runs it.
This change unifies configuration and service setup, making the application's lifecycle clearer. It's the foundation for the new middleware system.
// New webroot/index.php
use App\Application;
use Cake\Http\Server;
$server = new Server(new Application(dirname(__DIR__) . '/config'));
$server->emit($server->run());
Are there updates to Cake Console?
Yes, the console system gets a cleaner interface with the new Command interface. Commands now implement CommandInterface instead of extending Shell. The ConsoleIo class handles input/output, promoting better separation of concerns.
This matters because it aligns CakePHP with modern CLI library patterns and makes testing commands simpler. The old Shell class still works but is deprecated.
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\CommandInterface;
class MyCommand implements CommandInterface
{
public function execute(Arguments $args, ConsoleIo $io)
{
$io->out('Command executed.');
return null;
}
}
Cache and Log Engine Refinements
Cache and Log engine classes received updates for better consistency. The CacheEngine::get() method now requires a callable as its second argument for providing default values, matching the signature of Cache::remember().
For logging, the LogEngine::log() method is now strictly typed to accept only string levels. This prevents runtime errors from invalid level types.
Deprecations to be aware of
Several legacy features are marked for removal in CakePHP 4.0. Key deprecations include:
Dispatcherand related classes (useApplication).Controller::$modelClassproperty (useloadModel()).Network\CurlRequest(useHttp\Client).- Shell class-based console commands (use the new
Commandinterface).
Your application will still run, but you'll see deprecation notices. Addressing these now eases the future upgrade path.
FAQ
Is upgrading to CakePHP 3.8 from 3.7 risky?
No, it's a safe and recommended upgrade for most applications. The changes are largely additive, introducing new middleware and console interfaces while keeping full backwards compatibility. You will see deprecation notices for old features, but they will not break your app.
Do I have to use the new middleware system immediately?
No, the old dispatcher system still works. The middleware support is experimental in 3.8. You can adopt it gradually or wait until you upgrade to CakePHP 4.x, where it becomes the standard.
What happens to my existing Shell classes?
They continue to work but now trigger a deprecation warning. You should plan to migrate them to the new CommandInterface based system. The new system is more testable and follows a cleaner design.
Why was Table::findOrCreate() changed?
The behavior was refined to ensure it works correctly within transactions. The method now uses Table::saveOrFail() internally, which means it will throw an exception on failure instead of just returning false. This makes error handling more explicit.
Can I use PSR-15 middleware from other libraries?
Yes, that's a primary goal. The new MiddlewareQueue is fully compatible with the PSR-15 standard. You can integrate middleware packages from the broader PHP ecosystem directly into your CakePHP application flow.