承接 haruncpi/wp-api 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

haruncpi/wp-api

最新稳定版本:v1.0.1

Composer 安装命令:

composer require haruncpi/wp-api

包简介

An elegant WordPress REST API routing system

README 文档

README

An elegant WordPress REST API routing system.

WP API

Support

Buy Me A Coffee

Documentation

Installation

composer require haruncpi/wp-api

Configuration

In your plugin init file, write this simple config code.

ApiConfig::set_route_file( __DIR__ . '/api-routes.php' )
		->set_namespace( 'MyPlugin\Api' )
		->init();

Route Define

Open api-routes.php file and write route

Syntax

ApiRoute::get( $prefix, $endpoint, $callback, $auth = false );
ApiRoute::post( $prefix, $endpoint, $callback, $auth = false );

// Multiple route in a prefix group.
ApiRoute::prefix( $prefix, function( ApiRoute $route ) {
    $route->get( $endpoint, $callback, $auth = false );
    $route->post( $endpoint, $callback, $auth = false );
});

Where

  • $prefix is your plugin name with api version. Example: myplugin/v1
  • By default, $auth is false means the endpoint can be access without authentication.
  • To make a endpoint secure pass a callback in the place of $auth

Example

ApiRoute::get( 'myplugin/v1', '/me', 'ApiController@me' );

Secure route

ApiRoute::get( 'myplugin/v1', '/me', 'ApiController@me', 'AuthController@check' );

Various way to write callback.

ApiRoute::get( 'myplugin/v1', '/me', 'ApiController@me' );
ApiRoute::get( 'myplugin/v1', '/me', array( ApiController:class, 'me' ) );
ApiRoute::get( 'myplugin/v1', '/me', array( 'MyPlugin\Api\ApiController', 'me' ) );
ApiRoute::get( 'myplugin/v1', '/me', function() {
    // Do something.
});

Multiple route register

ApiRoute::prefix( 'myplugin/v1', function( ApiRoute $route ) {
    $route->get( '/products', 'ApiController@products' );
    $route->get( '/categories', 'ApiController@categories' );
});

With auth check

// With auth check
ApiRoute::prefix( 'myplugin/v1', function( ApiRoute $route ) {
    $route->get( '/me', 'ApiController@me' );
    $route->get( '/settings', 'ApiController@settings' );
    $route->post( '/logout', 'ApiController@logout' );
})->auth( 'AuthController@check' );

Plugin Example

API Plugin is a WordPress example plugin for this composer package.

统计信息

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

GitHub 信息

  • Stars: 12
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: cc-by-4.0
  • 更新时间: 2023-08-10