This repository has been archived by the owner. It is now read-only.

Extending

Kasper Isager edited this page May 19, 2015 · 10 revisions

Vanilla API allows you to easily integrate your own plugins and applications with the API Mapper - it's as simple as creating a new API class and putting it anywhere in your application or plugin where the Garden autoloader can find it.

API Classes

An API class is a small class that extends the abstract API Mapper and includes at least a register() method as defined by the API Interface. Endpoints are registered using the get(), post(), put() and delete() methods as outlined below:

/**
 * Foo API
 *
 * Description of Foo API
 *
 * @package   [name]
 * @since     [version]
 * @author    [author] <[email]>
 * @copyright [description]
 * @license   [url] [description]
 */
class FooAPI extends APIMapper {
  /**
   * Register API endpoints
   *
   * @since  [version]
   * @access protected
   * @param  array $data Request arguments passed along with the request
   * @return void
   * @static
   */
  protected static function register($data) {
    static::get("/[i:fooID]", [
      "application"  => "Baz" // (Optional) Required in case of non-unique controller
    , "controller"   => "Foo" // (Required) The controller to operate on
    , "method"       => "bar" // (Optional) Defaults to `index`
    , "authenticate" => true  // (Optional) Defaults to `true` for all write-methods
    , "arguments"    => []    // (Optional) Arguments to pass along to the method
    ]);
  }
}

Vanilla API uses AltoRouter and passes along the named parameters as method arguments which in the above route would correspond to $fooID. Additional method arguments can be supplied in the arguments key. These can be mapped to user input via the $data array if needed.

Extend existing class

To extend an existing API class, simply hook into the register event:

/**
 * Foo Hooks
 *
 * @package   [name]
 * @since     [version]
 * @author    [author] <[email]>
 * @copyright [description]
 * @license   [url] [description]
 */
class FooHooks implements Gdn_IPlugin {
  /**
   * Register extra Foo API endpoints
   *
   * @since  [version]
   * @access public
   * @param  FooAPI $sender The FooAPI instance
   * @return void
   * @static
   */
  public function FooAPI_register_handler($sender) {
    $sender::put('/bar/[i:fooID]', [...]);
  }
}
Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.