CakePHP5

Traits

Traits are a powerful feature in PHP that allow developers to reuse sets of methods in multiple classes without relying on traditional inheritance. In languages with single inheritance (like PHP), a class can only inherit from one parent class. This can sometimes be limiting when you want to share functionality across multiple, unrelated classes. Traits solve this problem by letting you "mix in" methods into different classes.

JSON

JavaScript Object Notation (JSON) is a text-based way of representing JavaScript object literals, arrays, and scalar data. It is language independent, self-describing, and plain text. It is a good solution for transferring data between systems because it is easy to read, write, and utilize.

CakePHP has built-in functions to serve data as JSON, making it trivial to allow access to data within an application or across the internet.

Behaviors

Behaviors are ways to enable the reuse of Model functions. For example, instead of adding our slug creation function into every model, which could be a lot of redundant code, we can create a Behavior to encapsulate the code and add the Behavior to our Model's initialize(), just like the Timestamp Behavior.

Seeders

Seeders are way to put data immediately into your tables. All you need to do is populate an array, then use that array to create rows in your table. You can use this data as a test, or to put in actual data without having to type it in multiple times or enter it in a form.

Migrations

Migrations are a method of coding your database schema for both table creation and change control tracking. They allow you build out your tables in code, and track updates to those tables, then use that code to create and update the database tables.

Disabled Accounts

Sometimes you need to disable a users account, without deleting the account and the information related to that account. That's why previously in Controller and Views there was a user role named "Disabled." You can set the role to Disabled currently, but it doesn't do anything. Now we're going to change that.

Unique Slugs

In our Database and Model tutorial we covered a very simple example to create a slug for use with a record. One problem with this example is that it doesn't create unique slugs, and since our database requires unique slugs, this could become an issue with two users having the same first and last name.

Neighbors

In CakePHP 2 there was a handy feature called "neighbors" where you could simply request the neighboring records (previous and next) for easy linking. This is not present since CakePHP 3, possibly due to the limited use of such a feature, however, with custom finders you can replicate the functionality yourself.

Optional Requirements

There may come a time when you have a form that has optional requirements. By that I mean that depending on a previous selection, future selections may become required, or not.

A simple method I've found to do this is with jQuery on the front end and no validation requirements on the back end. An example follows: