Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/haruncpi/wp-api
An elegant WordPress REST API routing system
https://github.com/haruncpi/wp-api
api api-library php rest-api wordpress wordpress-package wordpress-plugin-development
Last synced: about 2 months ago
JSON representation
An elegant WordPress REST API routing system
- Host: GitHub
- URL: https://github.com/haruncpi/wp-api
- Owner: haruncpi
- Created: 2023-08-10T10:00:52.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2023-12-15T05:43:57.000Z (about 1 year ago)
- Last Synced: 2024-11-09T09:10:00.295Z (about 2 months ago)
- Topics: api, api-library, php, rest-api, wordpress, wordpress-package, wordpress-plugin-development
- Language: PHP
- Homepage:
- Size: 879 KB
- Stars: 10
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
WP API
An elegant WordPress REST API routing system.
![WP API](wp-api.png)
## Documentation
### Installation
```
composer require haruncpi/wp-api
```### Configuration
In your plugin init file, write this simple config code.
```php
ApiConfig::set_route_file( __DIR__ . '/api-routes.php' )
->set_namespace( 'MyPlugin\Api' )
->init();
```### Route Define
Open `api-routes.php` file and write route
Syntax
```php
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
```php
ApiRoute::get( 'myplugin/v1', '/me', 'ApiController@me' );
```
Secure route
```php
ApiRoute::get( 'myplugin/v1', '/me', 'ApiController@me', 'AuthController@check' );
```### Various way to write callback.
```php
ApiRoute::get( 'myplugin/v1', '/me', 'ApiController@me' );
```
```php
ApiRoute::get( 'myplugin/v1', '/me', array( ApiController:class, 'me' ) );
```
```php
ApiRoute::get( 'myplugin/v1', '/me', array( 'MyPlugin\Api\ApiController', 'me' ) );
```
```php
ApiRoute::get( 'myplugin/v1', '/me', function() {
// Do something.
});
```### Multiple route register
```php
ApiRoute::prefix( 'myplugin/v1', function( ApiRoute $route ) {
$route->get( '/products', 'ApiController@products' );
$route->get( '/categories', 'ApiController@categories' );
});
```### With auth check
```php
// 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](https://github.com/haruncpi/api-plugin) is a WordPress example plugin for this composer package.