Class CBJsonController
Yii controllers exchanging JSON objects.
This base controller class allows developers to write controller actions which expect object parameters and return objects instead of printing/rendering them. It also has a uniform error handling mechanism which converts exceptions and PHP errors to standard-form jsons. The client application can be tuned to properly handle such exceptions.
This approach, apart from making writing actions as if they were naturally called by the client/consumer, allows for proper out-of-the-box documentation (i.e. through apigen).
Action signatures
All restful action signatures:- Accept at most one input parameter object. The input parameter name can be anything since there's at most one parameter.
- Return exactly one output object.
public function actionGetAll(NotificationQueryJson $notificationQuery) { return new NotificationJson(); }
Request/Input Objects
Input parameters are fetched in two different ways:
- Production mode: From the POST body as raw json object.
- Development mode: From the 'jsin' GET parameter as raw json object.
Input object types of a restful action can be:
- Scalars
- Objects of a CBJsonModel subtype
Request/Input Headers
Further input can be passed to a restful action through request headers:
- Production mode: Headers of the form 'this-is-some-header' are
accessed as 'thisIsSomeHeader':
this-is-some-header: someValue\r\n this-is-another-header: anotherValue\r\n
- Development mode: From the 'header' GET parameter as a hash:
http://.../jsin=...&header[thisIsSomeHeader]=someValue&header[thisIsAnotherHeader]=anotherValue
Response/Output objects
Output object types of a restful action can be:
- Scalars
- Objects of a CBJsonModel subtype
- Arrays of the above two element types
Response/Output errors
Errors are uniformly output using the createErrorObject method.
Examples
Here is an example GET request (for development mode) with both header and json:
http://.../index-test.php?r=controller/action&header[applicationId]=1&jsin={"id":34034,"name":"John Doe"}
- CController
-
CBJsonController
Direct known subclasses
ProductControllerAuthor: Konstantinos Filios <konfilios@gmail.com>
Since: 1.0
Located at components/CBJsonController.php
public
|
|
protected
string
|
|
protected
|
|
protected
|
|
protected
array
|
|
public
|
|
public
CAction
|
|
public
array
|
|
public
|
|
public
|
|
protected
|
|
protected
array
|
#
createErrorObject( integer $code, string $message, string $traceString, string $type )
Create a standard-form error object from passed details. |
public
boolean
|
$groupByToken | true |