Last december, the CakePHP team announced the immediate availability of 4.0.0. This release begins a new chapter for CakePHP, as 4.0 is now API stable. With this release, Cake 3.x moves into maintenance mode, while 2.x moves into security release mode.
The promise of the version is: cleaner, faster and still tasty as usual. I had the opportunity to bake a new application from scratch and I will give my feedback about my process.
Skeleton Design
The new version refreshes the skeleton design of the application.
Now we have 2 new folders on root:
-
Templates
The templates folder has presentational files placed here: elements, error pages, layouts, and view template files.
Pay attention for subfolders:
-
Core templates are lowercase: cell, element, email, layout
-
App templates still uppercase: Error, Pages
-
Resources
The resources folder has subfolders for various types of resource files.
The locales* sub folder stores string files for internationalization.
If you are familiar with i18n, you will see the difference:
-
src/Locale/pt_BR/default.po (3.x)
-
resources/locales/pt_BR/default.po (4.x)
Another important change was the .ctp files. They are moved for .php.
CakePHP template files have a default extension of .php now.
We have a new config/app_local.php file, which contains the configuration data that varies between environments and should be managed by configuration management, or your deployment tooling.
PHP Strict Type Mode
In PHP the declare (strict_types = 1); directive enables strict mode. In strict mode, only a variable of exact type of the “type declaration” will be accepted, or a TypeError will be thrown. The only exception to this rule is that an integer may be given to a function expecting a float.
This is a feature from PHP 7 - which we strongly recommended. All codebase from the skeleton and files generated by bake will include the function.
Entities
The preferred way of getting new entities is using the newEmptyEntity() method:
$product = $this->Products->newEmptyEntity();
Authentication
After 10 years baking, that's a really big change for me. I don't usually use plugins for authentication, I really like the Auth Component.
I think many bakers would agree, as I remember on the first international meetup, the co-host shared the same opinion.
The Auth Component is deprecated, so it's better move on and save the good memories.
The new way for implementing Authentication is more verbose. It requires a few steps, I won't go into detail about that, because you can easily check on book:
-
Install Authentication Plugin
-
Load the Plugin
-
Apply the Middleware
-
Load the Component
My first look is like I said, too verbose, for me anyway. We need to write a lot of code.
Also it is not included on the skeleton of CakePHP applications, you need include by your own.
https://book.cakephp.org/authentication/2/en/index.html
HTTPS Enforcer Middleware
Contrary to the Authentication, I was really surprised how easy it was to force my Application to use HTTPS. If you are familiar with CakePHP, you will use the Security Component for that:
class AppController extends Controller
{
public function initialize()
{
parent::initialize();
$this->loadComponent('Security', [
'blackHoleCallback' => 'forceSSL',
]);
}
public function beforeFilter(Event $event)
{
if (!Configure::read('debug')) {
$this->Security->requireSecure();
}
}
public function forceSSL()
{
return $this->redirect(
'https://' .
env('SERVER_NAME') .
Router::url($this->request->getRequestTarget())
);
}
}
The implementation on version 4 is less verbose and easy, kudos for the new version:
public function middleware(MiddlewareQueue $middlewareQueue)
{
$middlewareQueue
->add(new HttpsEnforcerMiddleware([
'redirect' => true,
'statusCode' => 302,
'disableOnDebug' => true,
]));
return $middlewareQueue;
}
What I know is a drop, what I don’t know is an ocean. The new version is here to stay, and this article it's a just one overview of basic usage of the new version.
* Version 4.1.0 is released already with more improvements and features.
Links
[1] Book https://book.cakephp.org/4/en/contents.html
[2] Migration Guide https://book.cakephp.org/4/en/appendices/migration-guides.html