Coder Social home page Coder Social logo

wp-api's Introduction

WP API

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.

wp-api's People

Contributors

haruncpi avatar harunollyo avatar maikuolan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.