kuliebiakin/simple-rest-api
Composer 安装命令:
composer require kuliebiakin/simple-rest-api
包简介
Simple WordPress REST API Router
关键字:
README 文档
README
WordPress REST API Router that is really easy to use.
Installation
Composer
$ composer require shtrihstr/simple-rest-api
Old way
Download archive from releases section
require_once '/path/to/Route.php'; require_once '/path/to/Router.php';
Usage
Create a Router
$router = new Simple_REST_API\Router( 'my-plugin/v1.0', [ 'etag' => true ] );
Example GET Route
Here is an example definition of a GET route:
$router->get( '/posts', function() { return get_posts(); } );
Dynamic Routing
Now you can create another controller for viewing individual blog posts:
$router->get( '/post/{id}', function( WP_REST_Response $response, $id ) { $post = get_post( $id ); if( ! $post ) { $response->set_status( 404 ); } else { $response->set_data( $post ); } return $response; } );
Example POST Route
POST routes signify the creation of a resource. An example for this is a feedback form.
$router->post( '/feedback', function( WP_REST_Request $request, WP_REST_Response $response ) { $body_params = $request->get_body_params(); $message = esc_html( $body_params['message'] ); wp_mail( 'feedback@yoursite.com', '[YourSite] Feedback', $message ); $response->set_status( 201 ); $response->set_data( 'Thank you for your feedback!' ); return $response; } );
Other methods
You can create controllers for most HTTP methods.
$router->put( '/post/{id}', function( $id ) { // ... } ); $router->delete( '/post/{id}', function( $id ) { // ... } ); $router->patch( '/post/{id}', function( $id ) { // ... } );
Route Variables
As it has been shown before, you can define variable parts in a route like this:
$router->get( '/post/{id}', function( $id ) { // ... } );
It is also possible to have more than one variable part, just make sure the closure arguments match the names of the variable parts:
$router->get( '/post/{post_id}/paged/{page_id}', function( $post_id, $page_id ) { // ... } );
While it's not recommended, you could also do this (note the switched arguments):
$router->get( '/post/{post_id}/paged/{page_id}', function( $page_id, $post_id ) { // ... } );
You can also ask for the current Request and Response objects:
$router->get( '/post/{id}', function( WP_REST_Request $request, WP_REST_Response $response, $id ) { // ... } );
Route Variable Converters
Before injecting the route variables into the controller, you can apply some converters:
$router->get( '/post/{id}', function( $id ) { // ... } )->convert( 'id', function( $id ) { return (int) $id; } );
This is useful when you want to convert route variables to objects:
$router->get( '/comments/{user}', function( $user ) { // ... } )->convert( 'user', function( $user ) { return get_user_by( 'id', $user ); } );
Requirements
The following will make sure the id argument is a positive integer since \d+ matches any amount of digits:
$router->get( '/post/{id}', function( $id ) { // ... } )->assert( 'id', '\d+' );
Middlewares
Route middlewares are added to routes and they are only triggered when the corresponding route is matched. You can also stack them:
$before_callback = function() { $GLOBALS['wpdb']->queries = []; }; $after_callback = function( WP_REST_Response $response ) { $data = $response->get_data(); if( is_array( $data ) ) { $data['debug'] = [ 'time' => timer_stop(), 'queries' => $GLOBALS['wpdb']->queries, ]; $response->set_data( $data ); } }; $router->get( '/post/{id}', function( $id ) { // ... } )->before( $before_callback )->after( $after_callback );
kuliebiakin/simple-rest-api 适用场景与选型建议
kuliebiakin/simple-rest-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.24k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 09 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「rest」 「api」 「wordpress」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kuliebiakin/simple-rest-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kuliebiakin/simple-rest-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kuliebiakin/simple-rest-api 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
A Flickr wrapper to allow you to call the Flickr api with Guzzle as the backend.Goal is to have 100% Flickr api coverage rather than just upload/display photos (currently at 23%).
Must-use plugin integrating WordPress with the Upsun platform: environment awareness, router-cache friendliness, safe preview clones, deploy migrations, Site Health checks, and a wp upsun CLI command.
BlockCypher's PHP SDK for REST API
Helper classes for creating cookie headers
统计信息
- 总下载量: 8.24k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-09-04