An elegant WordPress REST API routing system.
composer require haruncpi/wp-api In your plugin init file, write this simple config code.
ApiConfig::set_route_file( __DIR__ . '/api-routes.php' ) ->set_namespace( 'MyPlugin\Api' ) ->init();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
$prefixis your plugin name with api version. Example:myplugin/v1- By default,
$authis false means the endpoint can be access without authentication. - To make a endpoint
securepass 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' );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. });ApiRoute::prefix( 'myplugin/v1', function( ApiRoute $route ) { $route->get( '/products', 'ApiController@products' ); $route->get( '/categories', 'ApiController@categories' ); });// 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' );API Plugin is a WordPress example plugin for this composer package.

