Listener Options

The output produced by the listener is highly configurable using the Crud configuration options described in this section.

Either configure the options on the fly per action or enable them for all actions in your controller by adding them to your contoller’s initialize() event like this:

public function initialize()
{
  parent::initialize();
  $this->Crud->config('listeners.jsonApi.withJsonApiVersion', true);
}

withJsonApiVersion

Pass this mixed option a boolean with value true (default: false) to make the listener add the top-level jsonapi node with member node version to each response like shown below.

{
  "jsonapi": {
    "version": "1.0"
  }
}

Passing an array or hash will achieve the same result but will also generate the additional meta child node.

{
  "jsonapi": {
    "version": "1.0",
    "meta": {
      "cool": "stuff"
    }
  }
}

meta

Pass this array option (default: empty) an array or hash will make the listener add the the top-level jsonapi node with member node meta to each response like shown below.

{
  "jsonapi": {
    "meta": {
      "copyright": {
        "name": "FriendsOfCake"
      }
    }
  }
}

debugPrettyPrint

Setting this boolean option to false (default: true) will make the listener render non-pretty json in debug mode.

jsonOptions

Pass this array option (default: empty) an array with PHP Predefined JSON Constants to manipulate the generated json response. For example:

public function initialize()
{
    parent::initialize();
    $this->Crud->config('listeners.jsonApi.jsonOptions', [
        JSON_HEX_QUOT,
        JSON_UNESCAPED_UNICODE,
    ]);
}

include

Pass this array option (default: empty) an array with associated entity names to limit the data added to the json included node.

Please note that entity names:

  • must be lowercased
  • must be singular for entities with a belongsTo relationship
  • must be plural for entities with a hasMany relationship
$this->Crud->config('listeners.jsonApi.include', [
    'currency', // belongsTo relationship and thus singular
    'cultures', // hasMany relationship and thus plural
]);

Note

The value of the include configuration will be overwritten if the the client uses the ?include query parameter.

fieldSets

Pass this array option (default: empty) a hash with field names to limit the attributes/fields shown in the generated json. For example:

$this->Crud->config('listeners.jsonApi.fieldSets', [
    'countries' => [ // main record
        'name',
    ],
    'currencies' => [ // associated data
        'code',
    ],
]);

Note

Please note that there is no need to hide id fields as this is handled by the listener automatically as per the JSON API specification.

queryParameters

This array option allows you to specify query parameters to parse in your application. Currently this listener supports the official include parameter. You can easily add your own by specifying a callable.

$this->Crud->listener('jsonApi')->config('queryParameter.parent', [
    'callable' => function ($queryData, $subject) {
        $subject->query->where('parent' => $queryData);
    },
]);
  v: latest
Versions
latest
stable
Downloads
pdf
html
epub
On Read the Docs
Project Home
Builds
Downloads
On GitHub
View
Edit

Free document hosting provided by Read the Docs.