Skip to content

REST API

Pollora provides a clean and structured way to declare REST API routes for WordPress using PHP attributes. This system enables developers to define endpoints, specify HTTP methods, and implement permission handling in an intuitive way.

Pollora’s API routing system is based on PHP attributes, eliminating the need to manually register REST API routes in WordPress.

It consists of three key components:

  1. #[WpRestRoute]: Defines the base REST route.
  2. #[Method]: Specifies the HTTP method(s) for a given function.
  3. Permission Classes: Handles user permissions before executing the request.

To create a new API route, use the #[WpRestRoute] attribute on a class.

use Pollora\Attributes\WpRestRoute;
#[WpRestRoute(
namespace: 'app/v2',
route: 'document/(?P<documentId>\\d+)'
)]
class DocumentAPI {}
  • namespace: Defines the base namespace for the route.
  • route: Defines the endpoint pattern (supporting regex parameters).

Once defined, WordPress will recognize the API endpoint:

GET wp-json/app/v2/document/18

Use the #[Method] attribute on methods inside the class to define HTTP methods:

use Pollora\Attributes\WpRestRoute\Method;
use WP_REST_Request;
use WP_REST_Response;
class DocumentAPI
{
#[Method('GET')]
public function get(int $documentId): WP_REST_Response
{
return new WP_REST_Response([
'success' => true,
'documentId' => $documentId,
]);
}
#[Method(['POST', 'DELETE'])]
public function delete(WP_REST_Request $request, int $documentId): WP_REST_Response
{
return new WP_REST_Response([
'success' => true,
'deleted' => $documentId,
]);
}
}
  • GET
  • POST
  • PUT
  • DELETE
  • PATCH

If an invalid HTTP method is provided, an exception will be thrown during route registration.

Pollora allows defining permissions at both the class and method level.

Permissions can be applied globally to all methods within a class:

use Pollora\Attributes\WpRestRoute;
use Pollora\Attributes\WpRestRoute;
use Pollora\Attributes\WpRestRoute\Permissions\IsAdmin;
#[WpRestRoute(
namespace: 'app/v2',
route: 'document/(?P<documentId>\\d+)',
permissionCallback: IsAdmin::class
)]
class AdminDocumentAPI {}

Permissions can also be set for specific HTTP methods:

use Pollora\Attributes\WpRestRoute;
use Pollora\Attributes\WpRestRoute\Method;
use WP_REST_Response;
use Pollora\Attributes\WpRestRoute\Permissions\IsAdmin;
use Pollora\Attributes\WpRestRoute\Permissions\IsAuthor;
class AdminDocumentAPI
{
#[Method('GET', permissionCallback: IsAdmin::class)]
public function get(): WP_REST_Response {}
#[Method('DELETE', permissionCallback: IsAuthor::class)]
public function delete(): WP_REST_Response {}
}

If a method has its own permission callback, it overrides the class-level permission.

A permission class must implement Pollora\Attributes\WpRestRoute\Permission and define an allow() method that returns true, false, or a WP_Error.

use Pollora\Attributes\WpRestRoute\Permission;
use WP_REST_Request;
use WP_Error;
class IsAdmin implements Permission
{
public function allow(WP_REST_Request $request): bool|WP_Error
{
return current_user_can('manage_options') ?: new WP_Error(
'rest_forbidden',
__('You do not have permission to access this endpoint.'),
['status' => 403]
);
}
}
use Pollora\Attributes\WpRestRoute;
use Pollora\Attributes\WpRestRoute;
use Pollora\Attributes\WpRestRoute\Method;
use Pollora\Attributes\WpRestRoute\Permissions\IsAdmin;
use WP_REST_Request;
use WP_REST_Response;
#[WpRestRoute(
namespace: 'app/v2',
route: 'document/(?P<documentId>\\d+)',
permissionCallback: IsAdmin::class
)]
class DocumentAPI
{
#[Method('GET')]
public function get(int $documentId): WP_REST_Response
{
return new WP_REST_Response(['success' => true, 'documentId' => $documentId]);
}
#[Method(['DELETE', 'POST'])]
public function delete(WP_REST_Request $request, int $documentId): WP_REST_Response
{
return new WP_REST_Response(['success' => true, 'deleted' => $documentId]);
}
}
  1. Pollora scans attributes and detects classes annotated with #[WpRestRoute].
  2. It registers API endpoints dynamically within WordPress.
  3. Methods with #[Method] are linked to the appropriate HTTP method.
  4. Permissions are validated before executing the request.