register_rest_route is not passing me the callback function parameters

Raf*_*leo 0 rest wordpress routes

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();
Run Code Online (Sandbox Code Playgroud)

Here what I get when I run this endpoint

{"code":200,"message":"Horrayyy!!!!!!!","data":{}}
Run Code Online (Sandbox Code Playgroud)

Any Ideas why?

Raf*_*leo 5

找到了解决方案:

  public function wpfm_endpoints_cb(WP_REST_Request $request){
        $result['code'] = 200;
        $result['message'] = "Horrayyy!!!!!!!";
        $result['data'] = $request->get_params();

        return $result;
  }
Run Code Online (Sandbox Code Playgroud)