\CodeIgniter\APIResponseTrait

Response trait.

Provides common, more readable, methods to provide consistent HTTP responses under a variety of common situations when working as an API.

Summary

Methods
Properties
Constants
respond()
fail()
respondCreated()
respondDeleted()
respondNoContent()
failUnauthorized()
failForbidden()
failNotFound()
failValidationError()
failResourceExists()
failResourceGone()
failTooManyRequests()
failServerError()
$request
$response
No constants found
format()
$codes
N/A
No private methods found
No private properties found
N/A

Properties

$request

$request : \CodeIgniter\HTTP\IncomingRequest

Type

\CodeIgniter\HTTP\IncomingRequest

$response

$response : \CodeIgniter\HTTP\Response

Type

\CodeIgniter\HTTP\Response

$codes

$codes : array

Allows child classes to override the status code that is used in their API.

Type

array

Methods

respond()

respond(array|string|null  $data = null, integer  $status = null, string  $message = '') : mixed

Provides a single, simple method to return an API response, formatted to match the requested format, with proper content-type and status code.

Parameters

array|string|null $data
integer $status
string $message

Returns

mixed

fail()

fail(string|array  $messages, integer|null  $status = 400, string|null  $code = null, string  $customMessage = '') : mixed

Used for generic failures that no custom methods exist for.

Parameters

string|array $messages
integer|null $status

HTTP status code

string|null $code

Custom, API-specific, error code

string $customMessage

Returns

mixed

respondCreated()

respondCreated(mixed  $data = null, string  $message = '') : mixed

Used after successfully creating a new resource.

Parameters

mixed $data

Data.

string $message

Message.

Returns

mixed

respondDeleted()

respondDeleted(mixed  $data = null, string  $message = '') : mixed

Used after a resource has been successfully deleted.

Parameters

mixed $data

Data.

string $message

Message.

Returns

mixed

respondNoContent()

respondNoContent(string  $message = 'No Content') : mixed

Used after a command has been successfully executed but there is no meaningful reply to send back to the client.

Parameters

string $message

Message.

Returns

mixed

failUnauthorized()

failUnauthorized(string  $description = 'Unauthorized', string  $code = null, string  $message = '') : mixed

Used when the client is either didn't send authorization information, or had bad authorization credentials. User is encouraged to try again with the proper information.

Parameters

string $description
string $code
string $message

Returns

mixed

failForbidden()

failForbidden(string  $description = 'Forbidden', string  $code = null, string  $message = '') : mixed

Used when access is always denied to this resource and no amount of trying again will help.

Parameters

string $description
string $code
string $message

Returns

mixed

failNotFound()

failNotFound(string  $description = 'Not Found', string  $code = null, string  $message = '') : mixed

Used when a specified resource cannot be found.

Parameters

string $description
string $code
string $message

Returns

mixed

failValidationError()

failValidationError(string  $description = 'Bad Request', string  $code = null, string  $message = '') : mixed

Used when the data provided by the client cannot be validated.

Parameters

string $description
string $code
string $message

Returns

mixed

failResourceExists()

failResourceExists(string  $description = 'Conflict', string  $code = null, string  $message = '') : mixed

Use when trying to create a new resource and it already exists.

Parameters

string $description
string $code
string $message

Returns

mixed

failResourceGone()

failResourceGone(string  $description = 'Gone', string  $code = null, string  $message = '') : mixed

Use when a resource was previously deleted. This is different than Not Found, because here we know the data previously existed, but is now gone, where Not Found means we simply cannot find any information about it.

Parameters

string $description
string $code
string $message

Returns

mixed

failTooManyRequests()

failTooManyRequests(string  $description = 'Too Many Requests', string  $code = null, string  $message = '') : mixed

Used when the user has made too many requests for the resource recently.

Parameters

string $description
string $code
string $message

Returns

mixed

failServerError()

failServerError(string  $description = 'Internal Server Error', string|null  $code = null, string  $message = '') : \CodeIgniter\HTTP\Response

Used when there is a server error.

Parameters

string $description

The error message to show the user.

string|null $code

A custom, API-specific, error code.

string $message

A custom "reason" message to return.

Returns

\CodeIgniter\HTTP\Response

The value of the Response's send() method.

format()

format(string|array|null  $data = null) : string|null

Handles formatting a response. Currently makes some heavy assumptions and needs updating! :)

Parameters

string|array|null $data

Returns

string|null