# code-snippets/4.0.0-beta.1/php/REST_API/REST_Controller.php

Code Snippets, version 4.0.0-beta.1. 72 lines.

- Page: https://pluginprobe.com/plugins/code-snippets/4.0.0-beta.1/code/php/REST_API/REST_Controller.php
- Raw: https://pluginprobe.com/plugins/code-snippets/4.0.0-beta.1/raw/php/REST_API/REST_Controller.php
- Modified: 2026-06-19T15:56:12+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/code-snippets/4.0.0-beta.1/code/php/REST_API/REST_Controller.php#L10-L20`.

```php
<?php

namespace Code_Snippets\REST_API;

use WP_REST_Request;
use function Code_Snippets\code_snippets;
use const Code_Snippets\REST_API_NAMESPACE;

/**
 * Base class for REST API controllers.
 */
abstract class REST_Controller {
	/**
	 * The version of the REST API this controller belongs to.
	 *
	 * @var string
	 */
	public const VERSION = 0;

	/**
	 * The base route for this controller, relative to the REST API namespace and version.
	 *
	 * @var string
	 */
	public const BASE_ROUTE = '';

	/**
	 * The namespace of this controller's route.
	 *
	 * @var string
	 */
	protected string $namespace;

	/**
	 * Class constructor.
	 */
	public function __construct() {
		assert( static::VERSION > 0, get_class( $this ) . '::VERSION must be set' );
		assert( static::BASE_ROUTE, get_class( $this ) . '::BASE_ROUTE must be set' );

		$this->namespace = REST_API_NAMESPACE . static::VERSION;
		add_action( 'rest_api_init', [ $this, 'register_routes' ] );
	}

	/**
	 * Retrieve this controller's REST API base path, including namespace.
	 *
	 * @return string
	 */
	public static function get_base_route(): string {
		return REST_API_NAMESPACE . static::VERSION . '/' . static::BASE_ROUTE;
	}

	/**
	 * Register REST routes.
	 */
	abstract public function register_routes();

	/**
	 * Default permission callback for this controller's routes.
	 *
	 * @param WP_REST_Request $request Full data about the request.
	 *
	 * @return bool
	 *
	 * phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.Found
	 */
	public function permission_callback( WP_REST_Request $request ): bool {
		return code_snippets()->current_user_can();
	}
}

```
