sitecrafting / conifer

:evergreen_tree: A powerful WordPress library plugin for OO development
https://www.coniferplug.in
MIT License
18 stars 2 forks source link

REST API Router #38

Closed acobster closed 5 years ago

acobster commented 6 years ago

Example use case:

class MyController extends WP_REST_Controller {
  public function say_hi(WP_Rest_Request $request) {
    return sprintf("Hi, %s!", $request->get_param('name'));
  }
}

$controller = new MyController();
$router = new Conifer\Rest\Router;

$router->set_namespace('/my-project-namespace/v1')
  // GET /hi/coby -> "Hi, coby!"
  ->add_get('/hi/:name', [$controller, 'say_hi']);

See Adding Custom Endpoints. The calls to set_namespace and add_get would be the equivalent of:

register_rest_route('/my-project-namespace/v1', '/hi/(?P<name>\[a-z]+)', [
  'methods' => 'GET',
  'callback' => [$controller, 'say_hi'],
]);