I have a class to register and execute my custom endpoints.
If I register my endpoints outside a class I can get the parameters of my endpoint in my callback function. But inside a class the parameters are not passing
Here the code:
class MYPLUGIN_RestAPI {
public function __construct() {
add_action('rest_api_init', array($this, register_routes));
}
public function register_routes (){
register_rest_route (
'myplugin/v1',
'/book/(?P<id>[a-z0-9 .\-]+)',
array(
'methods' => 'GET',
'callback' => array($this, "endpoint_book_cb")
)
);
}
public function endpoint_book_cb($data){
$result['code'] = 200;
$result['message'] = "Horrayyy!!!!!!!";
$result['data'] = $data;
return $result;
}
}
new MYPLUGIN_RestAPI();
Here what I get when I run this endpoint
{"code":200,"message":"Horrayyy!!!!!!!","data":{}}
Any Ideas why?