Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions docs/listeners/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,35 @@ Response format

The default response format for both XML and JSON has two root keys, ``success`` and ``data``. It's possible to add
your own root keys simply by using ``_serialize`` on the view var.
For example you would like to add variable ``my_extra_data`` you have to add it to the configuration of the action like, this:

.. code-block:: php

<?php
class MyAction extends BaseAction

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of an example using a custom action class, an example which uses $this->Crud->action()->setConfig() would be more suitable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean like, so?

class SomeController extends AppController {

public function initialize() {
      $this->Crud->setConfig([
            'serialize' => [
                'my_extra_data'
            ],
            'api' => [
                'success' => [
                    'data' => [
                        'entity' => [],
                        'my_extra_data'
                    ]
                ]
            ]
        ]);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, though you missed the ->action() in the call chain and it should be done in beforeFilter() or controller's action method.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personnaly, I'm using it like this :

$this->set(compact('aggregation'));
$this->Crud->action()->setConfig('serialize.aggregation', 'aggregation');

{

protected $_defaultConfig = [
'serialize' => [
'my_extra_data'
],
// other stuff
];

protected function handle()
{
// do other stuff here
$this->_controller()->set('my_extra_data', 'This is my extra data I want to add to response');
}

The response will then look like:

.. code-block:: json
{
"success" : true,
"data" : {},
"my_extra_key" : "This is my extra data I want to add to response"


JSON response
^^^^^^^^^^^^^
Expand Down