These docs are for Crud v3 - that only works with CakePHP 2.x. For CRUD v4 docs please go to the new documentation site
The CRUD v4 documentation has been completely rewritten, and since CRUD v3 and CRUD v4 API is ~95% identical, it may be worth a look for CRUD v3 users as well until v4 docs are backported for v3.
The default API output is similar to CakePHP arrays. This is fine for private
APIs, but for public APIs that format isn’t consistent with other public APIs
such as Google, Twitter and GitHub. This listener allows you to transform the
API output. See the example below.
First make sure you have a working API using the ApiListener. The
ApiTransformationListener uses the ApiListener to activate on API requests.
As with all listeners you can add this listener by using the $components
It’s also possible to add the listener on the fly:
The ApiTransformationListener takes several configuration options:
Below is a detailed explanation of these options.
The changeNesting option removes the top model name for every record and
nests all associated records into the primary record.
Warning: When you have field names equal to the model names it will overwrite
See below for an example:
The changeKeys option replaces the model keys with their lowercase names. By
default it will derive these names from the model associations. hasMany and
hasAndBelongsToMany associations will get plural names while belongsTo and
hasOne will remain singular. The primary model will always be singular,
because there will only be one in every record. Example:
For flexibility you can also define your own replaceMap. This option will
then be used instead of the model associations. See further down on how to use
The changeTime option changes dates and datetimes to Unix time integers.
The castNumbers options changes numeric strings to real numbers. Example:
keyMethods & valueMethods
To avoid looping through everything multiple times, when you want to do your
own custom transformation the ApiTransformationListener allows you to hook
into the recursive loop method. This can be done by defining additional methods
in the keyMethods or valueMethods options. Their use is similar to the
call_user_func() syntax, the callback recieves both the value, and the path
(dot-delimited keys for the current array index) returning the updated value:
__After the methods __
As stated above you can override the replaceKeys map by setting the