概述
在编写端点时,使用控制器类来处理端点的功能可能会有所帮助。控制器类将提供一种与 API 交互的标准方式,同时也提供了一种更易于维护的与 API 交互的方式。WordPress 当前的最低 PHP 版本为 5.2,如果您正在开发将被 WordPress 生态系统广泛使用的端点,您应该考虑支持 WordPress 的最低要求。
PHP 5.2 没有内置命名空间。这意味着您声明的每个函数都将在全局范围内。如果您决定使用公共函数名(如 get_items())用于端点,而另一个插件也注册了该函数,PHP 将因致命错误而失败。这是因为函数 get_items() 被声明了两次。通过包装我们的端点,我们可以避免这些命名冲突,并且以一种一致的方式与 API 交互。
控制器
控制器通常只做一件事;它们接收输入并生成输出。对于 WordPress REST API,我们的控制器将处理请求输入作为 WP_REST_Request 对象,并生成响应输出作为 WP_REST_Response 对象。让我们看看一个示例控制器类:
class My_REST_Posts_Controller {
// Here initialize our namespace and resource name.
public function __construct() {
$this->namespace = '/my-namespace/v1';
$this->resource_name = 'posts';
}
// Register our routes.
public function register_routes() {
register_rest_route( $this->namespace, '/' . $this->resource_name, array(
// Here we register the readable endpoint for collections.
array(
'methods' => 'GET',
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
),
// Register our schema callback.
'schema' => array( $this, 'get_item_schema' ),
) );
register_rest_route( $this->namespace, '/' . $this->resource_name . '/(?P<id>[\d]+)', array(
// Notice how we are registering multiple endpoints the 'schema' equates to an OPTIONS request.
array(
'methods' => 'GET',
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
),
// Register our schema callback.
'schema' => array( $this, 'get_item_schema' ),
) );
}
/**
* Check permissions for the posts.
*
* @param WP_REST_Request $request Current request.
*/
public function get_items_permissions_check( $request ) {
if ( ! current_user_can( 'read' ) ) {
return new WP_Error( 'rest_forbidden', esc_html__( 'You cannot view the post resource.' ), array( 'status' => $this->authorization_status_code() ) );
}
return true;
}
/**
* Grabs the five most recent posts and outputs them as a rest response.
*
* @param WP_REST_Request $request Current request.
*/
public function get_items( $request ) {
$args = array(
'post_per_page' => 5,
);
$posts = get_posts( $args );
$data = array();
if ( empty( $posts ) ) {
return rest_ensure_response( $data );
}
foreach ( $posts as $post ) {
$response = $this->prepare_item_for_response( $post, $request );
$data[] = $this->prepare_response_for_collection( $response );
}
// Return all of our comment response data.
return rest_ensure_response( $data );
}
/**
* Check permissions for the posts.
*
* @param WP_REST_Request $request Current request.
*/
public function get_item_permissions_check( $request ) {
if ( ! current_user_can( 'read' ) ) {
return new WP_Error( 'rest_forbidden', esc_html__( 'You cannot view the post resource.' ), array( 'status' => $this->authorization_status_code() ) );
}
return true;
}
/**
* Grabs the five most recent posts and outputs them as a rest response.
*
* @param WP_REST_Request $request Current request.
*/
public function get_item( $request ) {
$id = (int) $request['id'];
$post = get_post( $id );
if ( empty( $post ) ) {
return rest_ensure_response( array() );
}
$response = prepare_item_for_response( $post );
// Return all of our post response data.
return $response;
}
/**
* Matches the post data to the schema we want.
*
* @param WP_Post $post The comment object whose response is being prepared.
*/
public function prepare_item_for_response( $post, $request ) {
$post_data = array();
$schema = $this->get_item_schema( $request );
// We are also renaming the fields to more understandable names.
if ( isset( $schema['properties']['id'] ) ) {
$post_data['id'] = (int) $post->ID;
}
if ( isset( $schema['properties']['content'] ) ) {
$post_data['content'] = apply_filters( 'the_content', $post->post_content, $post );
}
return rest_ensure_response( $post_data );
}
/**
* Prepare a response for inserting into a collection of responses.
*
* This is copied from WP_REST_Controller class in the WP REST API v2 plugin.
*
* @param WP_REST_Response $response Response object.
* @return array Response data, ready for insertion into collection data.
*/
public function prepare_response_for_collection( $response ) {
if ( ! ( $response instanceof WP_REST_Response ) ) {
return $response;
}
$data = (array) $response->get_data();
$server = rest_get_server();
if ( method_exists( $server, 'get_compact_response_links' ) ) {
$links = call_user_func( array( $server, 'get_compact_response_links' ), $response );
} else {
$links = call_user_func( array( $server, 'get_response_links' ), $response );
}
if ( ! empty( $links ) ) {
$data['_links'] = $links;
}
return $data;
}
/**
* Get our sample schema for a post.
*
* @param WP_REST_Request $request Current request.
*/
public function get_item_schema( $request ) {
$schema = array(
// This tells the spec of JSON Schema we are using which is draft 4.
'$schema' => 'http://json-schema.org/draft-04/schema#',
// The title property marks the identity of the resource.
'title' => 'post',
'type' => 'object',
// In JSON Schema you can specify object properties in the properties attribute.
'properties' => array(
'id' => array(
'description' => esc_html__( 'Unique identifier for the object.', 'my-textdomain' ),
'type' => 'integer',
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
),
'content' => array(
'description' => esc_html__( 'The content for the object.', 'my-textdomain' ),
'type' => 'string',
),
),
);
return $schema;
}
// Sets up the proper HTTP status code for authorization.
public function authorization_status_code() {
$status = 401;
if ( is_user_logged_in() ) {
$status = 403;
}
return $status;
}
}
// Function to register our new routes from the controller.
function prefix_register_my_rest_routes() {
$controller = new My_REST_Posts_Controller();
$controller->register_routes();
}
add_action( 'rest_api_init', 'prefix_register_my_rest_routes' );概述与未来
控制器类在开发端点时解决了我们面临的两个大问题:缺乏命名空间和一致的结构。重要的是要注意,您不应滥用端点的继承。例如:如果您为 posts 端点编写了一个控制器类(如上例所示),并希望支持自定义帖子类型,则不应像这样扩展您的 My_REST_Posts_Controller:class My_CPT_REST_Controller extends My_REST_Posts_Controller。
相反,您应该创建一个完全独立的控制器类,或者让 My_REST_Posts_Controller 处理所有可用的帖子类型。当您开始走向继承的黑暗深渊时,重要的是要明白,如果父类在任何时候必须更改,而您的子类依赖于它们,您将面临重大问题。在大多数情况下,您希望创建一个基础控制器类作为 interface 或 abstract class,您的每个端点控制器都可以实现或扩展它。abstract class 方法正被 WP REST API 团队采用,以便将 WP_REST_Controller 类潜在地纳入核心。
目前,“核心端点”支持帖子、帖子类型、帖子状态、修订版、分类法、术语、用户、评论和附件/媒体资源,正在一个功能插件中开发,该插件有望在某个时候移至 WordPress 核心。在该插件中有一个提议的 WP_REST_Controller 类,可用于构建您自己的端点控制器。WP_REST_Controller 具有许多优势,并且提供了一种创建 API 端点的一致方式。