Skip to content

Commit 118ac5c

Browse files
authored
Merge pull request #759 from FriendsOfCake/api
Fix ApiListener docs
2 parents 13a23f0 + 30d74ee commit 118ac5c

File tree

5 files changed

+27
-38
lines changed

5 files changed

+27
-38
lines changed

docs/listeners/api-pagination.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ it available for all your controllers, application wide.
2020
2121
public function initialize(): void
2222
{
23-
$this->loadComponent('RequestHandler');
2423
$this->loadComponent('Crud.Crud', [
2524
'listeners' => [
2625
'Crud.Api', // Required

docs/listeners/api-query-log.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ attach it to all controllers, application wide
4040
{
4141
public function initialize(): void
4242
{
43-
$this->loadComponent('RequestHandler');
4443
$this->loadComponent('Crud.Crud', [
4544
'listeners' => [
4645
'Crud.Api', // Required

docs/listeners/api.rst

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@ API
33

44
This listener allows you to easily create a JSON or XML Api built on top of Crud.
55

6-
Introduction
7-
------------
8-
.. note::
9-
10-
The ``API listener`` depends on the ``RequestHandler`` to be loaded **before** ``Crud``.
11-
12-
`Please also see the CakePHP documentation on JSON and XML views <http://book.cakephp.org/5/en/views/json-and-xml-views.html>`_
13-
146
Setup
157
-----
168

@@ -47,7 +39,6 @@ Attach it using components array, this is recommended if you want to attach it t
4739
4840
public function initialize(): void
4941
{
50-
$this->loadComponent('RequestHandler');
5142
$this->loadComponent('Crud.Crud', [
5243
'actions' => [
5344
'Crud.Index',
@@ -60,21 +51,9 @@ Attach it using components array, this is recommended if you want to attach it t
6051
Request detectors
6152
-----------------
6253

63-
The Api Listener creates 3 new detectors in your ``Request`` object.
64-
65-
is('json')
66-
^^^^^^^^^^
67-
68-
Checks if the extension of the request is ``.json`` or if the requester accepts json as part of the
69-
``HTTP accepts`` header.
70-
71-
is('xml')
72-
^^^^^^^^^
73-
74-
Checks if the extension of the request is ``.xml`` or if the requester accepts XML as part of the ``HTTP accepts``
75-
header.
54+
The Api Listener will add a new `api` detector to your ``ServerRequest`` class.
7655

77-
is('api')
56+
ServerRequest::is('api')
7857
^^^^^^^^^
7958

8059
Checking if the request is either ``is('json')`` or ``is('xml')``.
@@ -94,18 +73,32 @@ In case of an error, in order to get a standardized response in either
9473
``json`` or ``xml`` - according to the API request type, for `api` requests,
9574
the default CakePHP exception renderer needs to be overridden.
9675

97-
Set the ``Error.exceptionRenderer`` config in ``config/app.php`` to
98-
``\Crud\Error\ExceptionRenderer::class`` as following:
76+
Set the ``exceptionRenderer`` config for the ``ErrorHandlerMiddleware`` in your
77+
``Application::middleware()`` method in ``src/Application.php``.
9978

10079
.. code-block:: php
10180
102-
'Error' => [
103-
'errorLevel' => E_ALL,
104-
'exceptionRenderer' => \Crud\Error\ExceptionRenderer::class,
105-
'skipLog' => [],
106-
'log' => true,
107-
'trace' => true,
108-
],
81+
$middlewareQueue->add(
82+
new ErrorHandlerMiddleware(['exceptionRenderer' => \Crud\Error\ExceptionRenderer::class] + Configure::read('Error'), $this)
83+
)
84+
85+
You also need to update your ``ErrorController`` to use the ``JsonView`` or ``XmlView``
86+
classes for content negotiation.
87+
88+
.. code-block:: php
89+
90+
// src/Controller/ErrorController.php
91+
92+
use Crud\View\JsonView;
93+
use Crud\View\XmlView;
94+
95+
public function initialize(): void
96+
{
97+
$this->addViewClasses([
98+
'json' => JsonView::class,
99+
'xml' => XmlView::class,
100+
]);
101+
}
109102
110103
Request type enforcing
111104
----------------------

docs/quick-start.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ we do this by enabling Crud in the ``AppController`` with the ``actions`` option
3232
parent::initialize();
3333
3434
$this->loadComponent('Flash');
35-
$this->loadComponent('RequestHandler');
3635
$this->loadComponent('Crud.Crud', [
3736
'actions' => [
3837
'Crud.Index',

src/Listener/ApiListener.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ protected function _expandPath(Subject $subject, string $path): string
333333
}
334334

335335
/**
336-
* Inject view classes into RequestHandler
336+
* Add view classes to the controller
337337
*
338338
* @return void
339339
*/
@@ -345,8 +345,7 @@ public function injectViewClasses(): void
345345
/**
346346
* Get or set a viewClass
347347
*
348-
* `$type` could be `json`, `xml` or any other valid type
349-
* defined by the `RequestHandler`
348+
* `$type` could be `json`, `xml` or any other valid configured type
350349
*
351350
* `$class` could be any View class capable of handling
352351
* the response format for the `$type`. Normal

0 commit comments

Comments
 (0)