Friday 23 December 2016

What's new in Cakephp 3

I am a convinced user of CakePHP. I started with version 1 5 years ago and switched to version 2 when it came out. It is therefore quite natural that I look forward to the release of version 3.
This version has been in the pipes for a while already since the community has been talking about it for a long time and a preview version is released at the beginning of the year. After 3 versions of preview and 2 versions alpha, a first version beta has just made its appearance. I therefore offer you a short overview of the promises of the new version of the framework with the next major features.

PHP minimum version

CakePHP 3 will require at least version 5.4.19. The mbstring (encoding functionality), intl (date management functionality) and mcrypt (data encryption / decryption) extensions should be enabled.

Build on the standards of the PHP community

The new version will rely on new standards related to the development of PHP framework, including the PSR-0, PSR-1 and PSR-4 standards. These standards are developed by the "Framework Interop Group (FIG)" which aims to set standards of development and code writing between different PHP Framework projects (Symfony, CakePHP, Laravel, etc.) . PSR-0 mainly emphasizes naming rules for namespaces. PSR-1 mainly emphasizes naming for classes, methods and constants. PSR-4 highlights the autoload system and the organization of the files that this implies.
CakePHP can now be retrieved via Composer, which will then perform some configuration tasks on its own. You can also use Composer to load all kinds of plugins and third-party libraries. This will easily bring a lot of features to CakePHP.
The organization of the files and files of CakePHP has changed somewhat. On the left the organization of the files of versions 2.x and on the right the new organization for version 3:



In important points, we note that the model folder has considerably "inflated". The explanations of this new organization are found below (Table, Entity, etc.). A new Template folder makes its appearance, next to view. This is to separate the logic code from the display code. The Template folder contains all of the classic .ctp views, and the View folder contains Helpers and Cells (see below). There are also files related to Composer.

Using namespaces

First novelty in the CakePHP code, the use of namespaces. Appear in PHP version 5.3, they allow to group together classes, methods, constants that are linked. They thus avoid the collisions of classes or methods between, for example, created functionalities and an imported library. Two functions with the same name can be used at the same time if they are "stored" in their respective namespace. CakePHP files will now look like this: (example for a Controller)

Namespace App \ Controller; Class ArticlesController extends AppController { }

It may seem complicated at first to have to add these lines to each file but the console (Bake) will generate files with these lines. In addition, they are easily found by looking at the file tree. For example, for a Helper, the namespace would be App \ View \ Helper.

A lighter framework, with more plugins

Over time, CakePHP has enriched itself with many features. So the framework has become "heavy" from the point of view of the team in charge of its development. Indeed, each major development had to take into account these many features. CakePHP 3 will be lightened to keep only the essentials. However, all additional features will not be dropped. On the contrary, they will now be offered as, for example, plugins. The CakePHP team also wants this new functioning to boost the community by allowing more people to contribute to the development of plugins, without needing to enter the complete core of the framework.

A new ORM

CakePHP's ORM was often criticized. It is true that some requests could be complex. It was often necessary to play with the unbindModel and the behavior containers to arrive exactly to what one wanted. Some critics also criticized the fact that the ORM's functions only returned tables. The ORM has therefore been completely rewritten to overcome these problems and allow new functionalities.
All queries are processed via the new Query Builder object. Imagine the following model:



We will then have syntaxes of this type:

// Old version $ Articles = $ this-> Article-> find ('all', array ('Articles.validated' => 1), 'order' DESC ')); // CakePHP 3 $ Articles => this-> Articles-> find () -> where (array ('Articles.validated' => 1)) -> order (array ('Articles.created' => 'DESC'));

In this example, we mainly see differences in syntax, but this opens up very broad possibilities in terms of construction of queries. The options are really very numerous. You should also know that the Query Builder can "memorize" the various sub requests, doing things like that:

$ TenArticlesPublies = $ this-> Articles-> find () -> where (array ('Articles.validated' => 1)) -> limit (10);

// We reuse $ dixArticlesPublies $ TenLastArticles = $ tenArticlesPublies-> order (array ('Articles.created' => 'DESC')); $ DixPremiersArticles = $ dixArticlesPublies-> order (array ('Articles.created' => 'ASC'));


Indeed, queries are only actually done when you try to retrieve the data to process or display them. The Query Builder, as the name suggests, is used primarily to build queries more efficiently, with more possibilities than the old ORM.

To access the associated model data, we will do this as follows:

$ TenLastAuthorArticles = $ tenLastArticles-> contain (array ('Authors'));