Response
The Response class simplifies and standardizes your HTTP responses. This class is great when the response needs to be in JSON format.
Usage
Add the use
statement at the beginning of your PHP files to specify which Response class you’ll use in your code.
use WPMVC\Response;
Then initialize your response like this (by default it will return a failed response):
function ajax_response() { $response = new Response; }
Errors
The Response class has the functionality needed to keep track of the errors presented during the request execution.
Use the method $response->error()
to add an error to the response. The first parameter is a string that helps identify which value is giving the issue (use the identifier used to retrieve the value from the query string or request body), and the second parameter is the error message.
Use the properties $response->fails
and or $response->passes
after validations have been made to validate if the response is valid or not. This can be used to determine when to proceed with processing the request.
Set the property $response->success
after the request has been processed to indicate that the response is valid. This will return a valid response.
function ajax_request() { $response = new Response; // Using Request class $id = Request::input( 'id' ); $name = Request::input( 'name' ); // Validations and error reporting if ( empty( $id ) ) { $response->error( 'id', 'Required.' ); } if ( empty( $name ) ) { $response->error( 'name', 'Required.' ); } if ( strlen( $name ) < 8 ) { $response->error( 'name', 'Needs to have at least 8 characters.' ); } // Assert if response is valid // Assert if response has errors if ( $response->fails ) { // TODO code if fails } // Assert if response has NO errors if ( $response->passes ) { // TODO code if valid response // Finally response is set to be valid $response->success = true; } }
Use the property $response->message
to return response message.
function ajax_request() { $response = new Response; try { // TODO validation code if ( $response->passes ) { // TODO process request // Finally response is set to be valid $response->success = true; $response->message = 'Request processed!'; } } catch (Exception $e) { Log::error($e); $response->message = 'Fatal error ocurred.'; } }
The Response class has the functionality needed to render the response.
To finish and render the response as JSON, use method $response->json()
.
function ajax_request() { $response = new Response; try { // TODO code } catch (Exception $e) { Log::error($e); } // End and render JSON response $response->json(); }
You can also return the results using $response->to_array()
, $response->to_json()
or (string)$response
.
Add data
Use the property $response->data
add any data results to your response.
function ajax_request() { $response = new Response; try { // TODO code $children = Post::from( $post_id ); $response->success = true; $response->data = $children; } catch (Exception $e) { Log::error($e); } // End and render JSON response $response->json(); }
This is an example of how the response will look like:
{ "error": false, "status": 200, "data": [...] }[code id="541" title="Data in JSON response"]
Here is a list of additional properties that can be used in the response:
Property | Description |
$response->id | Used to set and return the ID of a record recently created. |
$response->redirect | Used to set and return a redirection URL. |
$response->query | Used to set and return any query information (i.e. paging information, records count, page number, limit, offsets and more). |