CakePHP 3 Tutorial 22: Serving JSON Data

Submitted by naidim on Sun, 05/21/2017 - 09:09

Serving JSON (JavaScript Object Notation) from CakePHP 3 is somewhat straight-forward, but there can be a few "gotchas" so I've written this tutorial to help others and as a reminder to myself. With the goal of presenting your CakePHP data as JSON data, here are the steps you need to follow. According to the CakePHP book ( https://book.cakephp.org/3.0/en/views/json-and-xml-views.html ) data views requires the RequestHandler component.

Tags

CakePHP 3 Tutorial 21: Conditional Validation

Submitted by naidim on Fri, 01/20/2017 - 12:21

CakePHP's validation is very robust, and while this is immensely helpful, getting to grips with all its possibilities can be daunting.

One handy feature is Conditional Validation. When adding a record, what if we want to ensure a field is not empty only when a matching field is also not empty? For example a schedule for work should have a start time and an end time, but if you wanted to schedule the day off it would have neither.

CakePHP 3 Tutorial 20: Obscure URLs

Submitted by naidim on Thu, 10/27/2016 - 09:33

By default CakePHP uses the unique Id of each record to view each record. For example http://hostname/application/model/view/6. This makes things simple and convenient, except when you want to allow an anonymous user to add and view their record, but no one else. Another user could simply iterate the id field and view all records in the table.

Obscurity is not real security, but can be enough in some instances, and is slightly better than no security at all.

CakePHP 3 Tutorial 19: Display One Thing, Save Another

Submitted by naidim on Tue, 10/25/2016 - 13:37

There may come a case, as in the previous tutorial (CakePHP 3 Tutorial 18: Autocomplete) that you want to display one thing in a form field, yet save a different value. In the previous case, you select a user's name from an autocomplete list, yet the user's id is the value you want to save in the database.

CakePHP 3 Tutorial 17: Adding Associated Records

Submitted by naidim on Tue, 10/25/2016 - 11:42

In the previous tutorial (CakePHP 3 Tutorial 16: Table Relationships) we set up associating tables and displaying associated data, but there's no point to displaying associated data if you can't add it first.

There are multiple ways to save associated data. We'll start with the easiest way first.

CakePHP 3 Tutorial 16: Table Relationships

Submitted by naidim on Fri, 10/21/2016 - 15:43

When working with CakePHP it is important to remember "Convention over Configuration." If you start from the bottom, or back end, the database, and work your way forward correctly, CakePHP will do much of legwork for you, saving tons of redundant and repetitive work. If you don't, there are almost always ways to make things work the hard way.

An example of this is the relationships between tables. If you name your database tables and fields following CakePHP convention, it becomes trivial to set up relationships between them.

CakePHP 3 Tutorial 15: Neighbors

Submitted by naidim on Tue, 10/18/2016 - 07:58

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 in CakePHP 3, probably due to the limited use of such a feature, however, with custom finders you can replicate the functionality.

In your model (e.g. src/Models/Table/UsersTable.php) add the following custom finder:

CakePHP 3 Tutorial 14: Optional Requirements

Submitted by naidim on Tue, 10/04/2016 - 12:21

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: