Rest API Controllers
Introduction
Onyx offers a simple RestController
class to facilitate the WordPress's REST API's. See more information at register_rest_route() and custom endpoints
All REST API controllers are created inside the ./core/app/Api
folder, with the namespace Onyx\Controllers
and extend the Onyx\RestController
class.
Registering
After creating the controller, it is necessary to register it in ./core/config/rest.php
as in the example below.
Parameters and Methods
Variable | Type | Description | Required |
---|---|---|---|
$namespace | string/array | Route Namespace | ✔ |
It is required to set the namespace
property as well as initialize the $this->initialize()
method as we can see in the following example.
Helpers Methods
Creating a REST API Controller
In this example we will create the endpoint /(api|wp-json)/onyx/v1/example
with the POST and GET methods.
Methods Helpers
route()
Variable | Type | Description | Required |
---|---|---|---|
$method | WP_Rest_Server/mixed | (GET, POST, PUT, PATCH, DELETE) | ✔ |
$route | string | Route endpoint | ✔ |
$callback | callable | Controller callback method to execute | ✔ |
$options | array | Endpoint options | ✘ |
$override | bool | Replace existing route. Default: falso | ✘ |
@return bool
@throws \Exception
If the $namespace
does not exist
rest_response()
Alias for \WP_REST_Response