2016-12-11 12 views
2

デフォルトのwp rest api V2エンドポイントを複製するにはどうすればよいですか?私は、デフォルトのエンドポイントと経路をそのまま維持したいが、私のアプリケーションに単純な応答を使用したい。デフォルトのWP REST API(v2)エンドポイントの重複

WordpressのV4.7

今の私のコード:また http://localhost/wp/wp-json/myrest/、(/ myrest)私の名前空間http://localhost/wp/wp-json/リストを呼び出す

function register_custom_routes() 
{ 
    $controller = new MY_REST_Posts_Controller; 
    $controller->register_routes(); 
} 

add_action('rest_api_init', 'register_custom_routes', 1); 

class MY_REST_Posts_Controller extends WP_REST_Controller { 
// this is a copy of default class WP_REST_Posts_Controller 
} 

は私を与える:

{ 
    "namespace": "myrest", 
    "routes": { 
    "/myrest": { 
     "namespace": "myrest", 
     "methods": [ 
     "GET" 
     ], 
    ... 
    "/myrest/(?P<id>[\\d]+)": { 
     "namespace": "myrest", 
     "methods": [ 
     "GET", 
     "POST", 
     "PUT", 
     "PATCH", 
     "DELETE" 
     ], 
    ... 
} 

けどhttp://localhost/wp/wp-json/myrest/posts(デフォルトのapiルートコールのような)の投稿をリストしようとすると動作しません:

{ 
    "code": "rest_no_route", 
    "message": "No route was found matching the URL and request method", 
    "data": { 
    "status": 404 
    } 
} 

アンドロイドアプリの簡易版を入手する必要がありますが、デフォルトの休憩のエンドポイントと経路をそのまま維持したいと考えています。

+0

解決方法を見つけましたか?私は同じものを探しています。 –

+0

解決策は見つかりましたか? –

答えて

1

ここに解決策があります。私はwpプラグインでコードをラップしました。

class WP_REST_custom_controller extends WP_REST_Controller { 

    // this is a copy of default class WP_REST_Posts_Controller 


    // Edited constructor for cutom namespace and endpoint url 
    /** 
    * Constructor. 
    * 
    * @since 4.7.0 
    * @access public 
    * 
    * @param string $post_type Post type. 
    */ 
    public function __construct() { 

     $this->post_type = 'post'; 
     $this->namespace = 'custom_namespace/v1'; 
     $obj = get_post_type_object($post_type); 
     $this->rest_base = ! empty($obj->rest_base) ? $obj->rest_base : $obj->name; 

     $this->resource_name = 'posts'; 

     $this->meta = new WP_REST_Post_Meta_Fields($this->post_type); 
    } 


    // this is a copy of default class WP_REST_Posts_Controller with necessary edits 

} 

// Function to register our new routes from the controller. 
function register_custom_rest_routes() { 
    $controller = new WP_REST_custom_controller(); 
    $controller->register_routes(); 
} 

add_action('rest_api_init', 'register_custom_rest_routes'); 
関連する問題