WordPress REST API REGISTER_REST_ROUTE出现500错误

时间:2020-04-06 作者:sialfa

我想创建一个类来注册我的自定义主题rest路由。在我编写了这段代码之后,我注意到RESTAPI将给出500个错误,而axios无法获取数据。我想通过使用vue创建一个单页wordpress网站来获取页面内容,但目前我需要解决这个问题。有人能帮我吗?我的代码正确吗?

class Uheme_Rest_Routes {

  public static function init()
  {
    add_action( \'rest_api_init\', array( __CLASS__, \'uheme_routes\') );
  }

  public function uheme_routes()
  {
    register_rest_route(
      \'uheme/v1\',
      \'/menu\',
      array(
        \'method\' => \'GET\',
        \'callback\' => array($this, \'_rest_menu\')
      )
    );

  }

  public function _rest_menu()
  {
    return wp_get_nav_menu_items(\'menu\');
  }

}
Uheme_Rest_Routes::init();

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

Revised answer

To other readers, another issue was the OP used the wrong route.

So make sure to make the API request to the correct route, which is a combination of the first two parameters for register_rest_route() (namespace + route base). So with register_rest_route( \'uheme/v1\', \'/menu\' ), the route is uheme/v1/menu. Additionally:

  1. With permalinks enabled, you can get a pretty endpoint URL (for the above route) like https://example.com/wp-json/uheme/v1/menu

  2. But regardless permalinks enabled or disabled, the endpoint is also accessible at index.php?rest_route=<route> as in https://example.com/index.php?rest_route=/uheme/v1/menu

And to OP, if a callable/callback is in the form of array( __CLASS__, \'method_name\' ) (or maybe \'My_Class::method_name\'), then you need to define the class method as static (e.g. public static function method_name()) to prevent PHP errors. :)

And note that you also made a mistake with that \'method\' => \'GET\' which should be \'methods\' => \'GET\' — note the plural "methods". For GET (request) method, that method would work because GET is the default method, but if you were only allowing POST method and you used \'method\' => \'POST\', then that wouldn\'t work — POST would never be allowed! So once again, the correct array key is methods.


Original answer

Your original class (without extending WP_REST_Routes) worked for me, after I changed the $this to __CLASS__:

\'callback\' => array($this, \'_rest_menu\') // I changed this
\'callback\' => array(__CLASS__, \'_rest_menu\') // to this

Without that change, I got this error:

{"code":"rest_invalid_handler","message":"The handler for the route is invalid","data":{"status":500}}

So make sure you provide a valid callable/callback as the route handler. Or that be aware that in your utheme_routes() method, the $this is not available because you didn\'t do something like $instance = new Uheme_Rest_Routes; $instance->init();.

Update: You can also try changing the \'method\' => \'GET\' to \'methods\' => \'GET\'. And be sure the route is /wp-json/uheme/v1/menu.