@@ -8,9 +8,8 @@ Introduction
88
99This module provides data structures and rendering for the API-Problem format.
1010
11- - [ Problem API] ( http://tools.ietf.org/html/draft-nottingham-http-problem-05 ) ,
12- used for reporting API problems
13-
11+ - [ Problem Details for HTTP APIs] ( http://tools.ietf.org/html/draft-nottingham-http-problem-06 ) ,
12+ used for reporting API problems.
1413
1514Installation
1615------------
@@ -80,7 +79,7 @@ function:
8079 'ZF\ApiProblem\Listener\SendApiProblemResponseListener' => 'ZF\ApiProblem\Factory\SendApiProblemResponseListenerFactory',
8180 'ZF\ApiProblem\View\ApiProblemRenderer' => 'ZF\ApiProblem\Factory\ApiProblemRendererFactory',
8281 'ZF\ApiProblem\View\ApiProblemStrategy' => 'ZF\ApiProblem\Factory\ApiProblemStrategyFactory',
83- )
82+ ),
8483),
8584'view_manager' => array(
8685 // Enable this in your application configuration in order to get full
@@ -98,40 +97,42 @@ ZF2 Events
9897
9998The ` ApiProblemListener ` attaches to three events in the MVC lifecycle:
10099
101- - ` MvcEvent::EVENT_RENDER ` with a priority of 1000
102- - ` MvcEvent::EVENT_DISPATCH_ERROR ` with a priority of 100
103- - ` MvcEvent::EVENT_DISPATCH ` as a _ shared _ event on ` Zend\Stdlib\DispatchableInterface ` with a
104- priority of 100
100+ - ` MvcEvent::EVENT_DISPATCH ` as a _ shared _ listener on ` Zend\Stdlib\DispatchableInterface ` with a
101+ priority of ` 100 ` .
102+ - ` MvcEvent::EVENT_DISPATCH_ERROR ` with a priority of ` 100 ` .
103+ - ` MvcEvent::EVENT_RENDER ` with a priority of ` 1000 ` .
105104
106- If the current Accept media type does not match the configured api-problem media types,
107- then this listener passes on taking any action.
105+ If the current ` Accept ` media type does not match the configured API-Problem media types (by
106+ default, these are ` application/json ` and ` application/*+json ` ), then this listener returns without
107+ taking any action.
108108
109109When this listener does take action, the purposes are threefold:
110110
111111- Before dispatching, the ` render_error_controllers ` configuration value is consulted to determine
112- if the ` ZF\ApiProblem\Listener\RenderErrorListener ` should be attached, see
113- ` RenderErrorListener ` for more information.
114- - After dispatching, detect the type of response from the controller, if it is already an
115- ApiProblem model, continue. If not, but there was an exception throw during dispatch,
116- convert the response to an api-problem with some information as to what the exception is.
117- - If a dispatch error has occurred, and is a valid Accept type, attempt to cast the dispatch
118- exception into an API problem response.
112+ if the ` ZF\ApiProblem\Listener\RenderErrorListener ` should be attached; see
113+ [ RenderErrorListener] ( #rendererrorlistener ) for more information.
114+ - After dispatching, detects the type of response from the controller; if it is already an
115+ ` ApiProblem ` model, it continues without doing anything. If an exception was thrown during
116+ dispatch, it converts the response to an API-Problem response with some information from the
117+ exception.
118+ - If a dispatch error occurred, and the ` Accept ` type is in the set defined for API-Problems, it
119+ attempts to cast the dispatch exception into an API-Problem response.
119120
120121#### ` ZF\ApiProblem\Listener\RenderErrorListener `
121122
122- This listener is attached to ` MvcEvent::EVENT_RENDER_ERROR ` at priority ` 100 ` . This listener
123- is conditionally attached by ` ZF\ApiProblem\Listener\ApiProblemListener ` for controllers that
124- require API Problem responses. With a priority of ` 100 ` , this ensures that this Render Error
125- time listener will run before the ZF2 one. In cases when it does run, it will attempt to
126- take exceptions and inject an api -problem response in the HTTP response.
123+ This listener is attached to ` MvcEvent::EVENT_RENDER_ERROR ` at priority ` 100 ` . This listener is
124+ conditionally attached by ` ZF\ApiProblem\Listener\ApiProblemListener ` for controllers that require
125+ API Problem responses. With a priority of ` 100 ` , this ensures that this listener runs before the
126+ default ZF2 listener on this event. In cases when it does run, it will cast an exception into an
127+ API -problem response.
127128
128129#### ` ZF\ApiProblem\Listener\SendApiProblemResponseListener `
129130
130- This listener is attached to the ` SendResponseEvent::EVENT_SEND_RESPONSE ` at priority ` -500 ` .
131- The primary purpose of this listener is to, when an ApiProblem response is to be sent, to
132- send the headers and content body. This differs from the way ZF2 typically sends responses
133- in that inside this listener it takes into account if the Response should include an
134- exception trace as part of the serialized response or not .
131+ This listener is attached to ` SendResponseEvent::EVENT_SEND_RESPONSE ` at priority ` -500 ` . The
132+ primary purpose of this listener is, on detection of an API-Problem response, to send appropriate
133+ headers and the problem details as the content body. If the ` view_manager ` 's ` display_exceptions `
134+ setting is enabled, the listener will determine if the API-Problem represents an application
135+ exception, and, if so, inject the exception trace as part of the serialized response.
135136
136137ZF2 Services
137138------------
@@ -146,56 +147,73 @@ ZF2 Services
146147
147148#### ` ZF\ApiProblem\View\ApiProblemRenderer `
148149
149- This service extends the JsonRenderer service from the ZF2 MVC layer. It's primary responsibility
150+ This service extends the ` JsonRenderer ` service from the ZF2 MVC layer. Its primary responsibility
150151is to decorate JSON rendering with the ability to optionally output stack traces.
151152
152153#### ` ZF\ApiProblem\View\ApiProblemStrategy `
153154
154- This service is a view strategy that allows any ApiProblemModels details to be injected into the
155- MVC's HTTP response object. This is similar in nature to the ` JsonStrategy ` .
155+ This service is a view strategy that detects a ` ZF\ApiProblem\View\ApiProblemModel ` ; when detected,
156+ it selects the [ ApiProblemRender] ( #zfapiproblemviewapiproblemrenderer ) , and injects the response
157+ with a ` Content-Type ` header that contains the ` application/problem+json ` media type. This is
158+ similar in nature to Zend Framework 2's ` JsonStrategy ` .
156159
157160### Models
158161
159162#### ` ZF\ApiProblem\ApiProblem `
160163
161- An instance of ` ZF\ApiProblem\ApiProblem ` serves the purpose of modeling the kind of problem that
162- is encountered. An instance of ` ApiProblem ` is typically wrapped in an ` ApiProblemResponse `
163- (see ` ApiProblemResponse ` below). Most information can be passed into the constructor:
164+ An instance of ` ZF\ApiProblem\ApiProblem ` serves the purpose of modeling the kind of problem that is
165+ encountered. An instance of ` ApiProblem ` is typically wrapped in an
166+ [ ApiProblemResponse] ( #zfapiproblemapiproblemresponse ) . Most information can be passed into the
167+ constructor:
164168
165169``` php
166170class ApiProblem {
167- public function __construct($status, $detail, $type = null, $title = null, array $additional = array()) {}
171+ public function __construct(
172+ $status,
173+ $detail,
174+ $type = null,
175+ $title = null,
176+ array $additional = array()
177+ ) {
178+ /* ... */
179+ }
168180}
169181```
170182
171183For example:
172184
173185``` php
174186new ApiProblem(404, 'Entity not found');
187+
175188// or
189+
176190new ApiProblem(424, $exceptionInstance);
177191```
178192
179193#### ` ZF\ApiProblem\ApiProblemResponse `
180194
181- An instance of ` ZF\ApiProblem\ApiProblem ` can be returned from any controller service or ZF2
182- MVC event. When it is, the HTTP response will be converted to the proper JSON based HTTP response.
195+ An instance of ` ZF\ApiProblem\ApiProblemResponse ` can be returned from any controller service or ZF2
196+ MVC event in order to short-circuit the MVC lifecycle and immediately return a response. When it
197+ is, the response will be converted to the proper JSON structure for an API-Problem, and the
198+ ` Content-Type ` header will be set to the ` application/problem+json ` media type.
183199
184200For example:
185201
186202``` php
203+ use Zend\Mvc\Controller\AbstractActionController;
187204use ZF\ApiProblem\ApiProblem;
188205use ZF\ApiProblem\ApiProblemResponse;
189- class MyController extends Zend\Mvc\Controller\AbstractActionController
206+
207+ class MyController extends AbstractActionController
190208{
191209 /* ... */
192210 public function fetch($id)
193211 {
194212 $entity = $this->model->fetch($id);
195- if (!$entity) {
213+ if (! $entity) {
196214 return new ApiProblemResponse(ApiProblem(404, 'Entity not found'));
197215 }
198216 return $entity;
199217 }
200218}
201- ```
219+ ```
0 commit comments