shtrihstr/simple-rest-api 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

shtrihstr/simple-rest-api

Composer 安装命令:

composer require shtrihstr/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 );

shtrihstr/simple-rest-api 适用场景与选型建议

shtrihstr/simple-rest-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.3k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2016 年 05 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「rest」 「api」 「wordpress」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 shtrihstr/simple-rest-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 shtrihstr/simple-rest-api 我们能提供哪些服务?
定制开发 / 二次开发

基于 shtrihstr/simple-rest-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.3k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 2
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-05-03