admin管理员组文章数量:1390749
I get a rest_no_route
error if I try to use a custom endpoint I've created. Is there any solution? I'm creating my theme routes inside a plugin class, but I don't think this is a problem.
I'm using axios to get the data on front-end.
class theme_Rest_Routes extends WP_REST_Controller{
public static function init()
{
add_action( 'rest_api_init', array( __CLASS__, 'register_routes') );
}
public function register_routes()
{
register_rest_route( 'theme/v1', '/menu/', array(
'methods' => 'GET',
'callback' => array(__CLASS__, 'theme_rest_menu'))
);
}
public function theme_rest_menu( $request )
{
return wp_get_nav_menu_items('menu');
}
}
theme_Rest_Routes::init();
I get a rest_no_route
error if I try to use a custom endpoint I've created. Is there any solution? I'm creating my theme routes inside a plugin class, but I don't think this is a problem.
I'm using axios to get the data on front-end.
class theme_Rest_Routes extends WP_REST_Controller{
public static function init()
{
add_action( 'rest_api_init', array( __CLASS__, 'register_routes') );
}
public function register_routes()
{
register_rest_route( 'theme/v1', '/menu/', array(
'methods' => 'GET',
'callback' => array(__CLASS__, 'theme_rest_menu'))
);
}
public function theme_rest_menu( $request )
{
return wp_get_nav_menu_items('menu');
}
}
theme_Rest_Routes::init();
Share
Improve this question
asked Apr 6, 2020 at 12:58
sialfasialfa
32910 silver badges29 bronze badges
1
|
1 Answer
Reset to default 0The problem is, you are calling a static method from inside of a non-static method. The following will work.
class theme_Rest_Routes extends WP_REST_Controller{
public static function init() {
$instance = new self();
add_action( 'rest_api_init', array( $instance, 'register_routes' ) );
}
public function register_routes() {
register_rest_route( 'theme/v1', '/menu/', array(
'methods' => 'GET',
'callback' => array( $this, 'theme_rest_menu' ) )
);
}
public function theme_rest_menu( $request ) {
return wp_get_nav_menu_items( 'menu' );
}
}
theme_Rest_Routes::init();
Code Improvement
You should not hook into rest_api_init
from within the controller class itself, rather call it from an external class/function. Also class name should use capitalized words. As like below:
function theme_register_rest_apis() {
$controller = new Theme_Rest_Routes();
$controller->register_routes();
}
add_action( 'rest_api_init', 'theme_register_rest_apis' );
class Theme_Rest_Routes extends WP_REST_Controller{
public function register_routes() {
register_rest_route( 'theme/v1', '/menu/', array(
'methods' => 'GET',
'callback' => array( $this, 'theme_rest_menu' ) )
);
}
public function theme_rest_menu( $request ) {
return wp_get_nav_menu_items( 'menu' );
}
}
本文标签: ajaxrestnoroute custom route
版权声明:本文标题:ajax - rest_no_route custom route 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744592452a2614576.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
WP_REST_Controller
, in fact in this situation it is either dead weight, or, causing harm as you don't use any of the Rest controller class. In both your question, and the answer below, you can deleteextends WP_REST_Controller
and it will work the same – Tom J Nowell ♦ Commented Apr 6, 2020 at 15:03