Skip to content

Added example for extra key in ApiListener Action#657

Open
gringlas wants to merge 3 commits into
FriendsOfCake:masterfrom
gringlas:patch-3
Open

Added example for extra key in ApiListener Action#657
gringlas wants to merge 3 commits into
FriendsOfCake:masterfrom
gringlas:patch-3

Conversation

@gringlas
Copy link
Copy Markdown
Contributor

I thought the documentation could get a bit improved here with an example of how exactly add an extra key to response, as up for today I never really figured it out and even then it took me some trying ;)

I thought the documentation could get a bit improved here with an example of how exactly add an extra key to response, as up for today I never really figured it out and even then it took me some trying ;)
Comment thread docs/listeners/api.rst Outdated
.. 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');

I've update the code example to searilize variable in beforeFilter ans also added the method of doing that in a controller action, thanks to @irongomme.
@gringlas
Copy link
Copy Markdown
Contributor Author

Why did those checks fail?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants