hametuha / rest-pattern
用于轻松创建基于WordPress的REST API模式的类
1.1.1
2022-12-05 06:30 UTC
Requires
- php: >=5.6
- hametuha/singleton-pattern: ^1.2
Requires (Dev)
- phpunit/phpunit: >=5.7
- yoast/phpunit-polyfills: ^1.0
This package is auto-updated.
Last update: 2024-09-05 10:11:20 UTC
README
Rest API Pattern类,用于轻松创建基于WordPress的REST API。
安装
composer require haemtuha/rest-pattern
然后,在您的主题或插件中包含autoload.php。
实现
这个库是REST API的抽象类。如下继承这个类
<?php namespace Vendor\Library\RestApi; use Hametuha\RestPattern\RestApiBase; class UsersApi extends RestApiBase { protected $namespace = 'vendor'; protectd $route = 'user/(?P<user_id>\d+)'; protected function get_args( $request ) { return [ 'user_id' => [ 'required' => true, 'validate_callback' => function( $var ) { return $var && get_userdata( $var ); } ], ]; } protected function handle_get( $request ) { return [ 'success' => true, 'user' => get_userdata( $request->get_param( 'user_id' ) ), ]; } }
handle_***方法将处理您的请求。它们应该返回可转换为JSON的对象(数组、对象),WP_REST_Restponse,或WP_Error。或者,您可以在请求处理程序中抛出Exception,然后将被自动转换为WP_Error。
许可证
GPL 3.0或更高版本。