Skip to content
CakePHP PHP

Looking Ahead to CakePHP 4.0

5 min read
Strawberries

Last week the first alpha release of CakePHP 4.0 was made available for testing. This means that the next major version of CakePHP is getting nearer. This is a good opportunity to look ahead at the new and changing functionality of the next version of this PHP framework.

Database Timezone

If you find yourself in the situation where your database’s timezone differs from your app’s you’ll be able to start using DateTime::setTimezone(). The ORM will then automatically handle the timezone conversion between the two.

Error Pages

The ErrorController will start taking prefixed routes into account and will attempt to use a template associated with the prefix before falling back on the app’s default error pages. This will be very useful for people who use route prefixing for things like an admin area on a website. It means you will be able to have different templates for the main website and admin area.

Views

CakePHP 4.0 is going to see a bit of restructuring with where and how view templates reside within your app. View templates will be moved from src/Template/ to templates/ in the root of either your app or a plugin and the use of .ctp as the file extension for CakePHP templates is being dropped in favour of .php. For example, if you have a page template like:

src/Template/Pages/home.ctp

It will want moving to:

template/Pages/home.php

The special template folders are all being renamed to a lowercase form: so Cell, Element, Email and Layout will become cell, element, email and layout respectively.

FormHelper

There’s a couple of improvements coming to the FormHelper. Firstly, it will generate HTML5 validation messages for fields marked as required in an entity’s ORM table class; and secondly, datetime fields will produce <input> elements of the type datetime-local.

I particularly like this second improvement as I’ve never been a big fan of CakePHP’s date selectors, but will likely continue to replace these with my own controls for production apps. datetime-local browser support is still not universal. For example, neither Firefox or Safari support it currently on their desktop versions. However, it is likely to be useful for scaffolding an app.

Routes

There are a few changes coming to the handling of routes in CakePHP 4.0.

Dashes are going to be used instead of underscores for routes by default. This will affect RouteBuilder::resources(), Router::plugin() and Router::prefix(). This is a good move in my opinion as underscores in URLs are not especially user-friendly. If you really need to revert to the existing CakePHP 3 underscored approach then there will be options available to enable this.

The matching of controller method names to invoke actions is going to become case-sensitive. That means passing resetpassword in the URL will no longer match a controller’s resetPassword method (note the capital ‘P’).

The build() method of the UrlHelper will no longer accept a boolean as the second parameter to output a full URL including the domain. Instead you will need to pass ['fullBase' => true]. This should lead to more readable code in the long run.

PSR-15

Support for the PSR-15 HTTP Server Request Handlers specification is being added to CakePHP 4.0. This means that middlewares now implement the Psr\Http\Server\MiddlewareInterface.

PSR-18

The PSR-18 HTTP Client specification is going to be followed from CakePHP 4 onwards.

Cake\Http\Client\Response::isSuccess() is being added which will return true for any of the 2xx response codes. Cake\Http\Client\Response::isOk() will return true for all 2xx and 3xx response codes.

Deprecations

Many things have been deprecated during the CakePHP 3 run of releases. These will be removed in CakePHP 4.0. This trend will continue with the next release.

What looks to be one of the biggest deprecations in CakePHP 4.0 is with the Filesystem package that has been part of CakePHP since version 1. That means if you’re currently using either the File or Folder utility classes then you want to start looking for alternatives. They won’t stop working until CakePHP 5.0, but are going to start throwing deprecation warnings in CakePHP 4.0 onwards.

Further Information

There’s still a way to go before a stable version of CakePHP 4.0 is released, but this list of new features and changes shouldn’t change too much between then and now. For a complete list of the changes coming with CakePHP 4.0 take a look at the migration guide.

One last thing, while CakePHP 3 has been known as Red Velvet, CakePHP 4 is going to be Strawberry (hence the choice of picture for this blog post).

You can follow @cakephp on Twitter for updates on future releases of the framework. If you’ve found this post useful you might also want to give me a follow, @drmonkeyninja.

© 2024 Andy Carter