moxie-lean / wp-endpoints-routes

获取路由集合。

2.0.2 2016-06-03 16:28 UTC

This package is auto-updated.

Last update: 2024-08-28 10:40:10 UTC


README

获取路由集合并通过WP-API公开。此扩展将默认创建一个端点(在`/wp-json/lean/v1/routes`)。

当前状态

目前端点仅自动添加页面。您可以使用`ln_endpoints_data_routes`过滤器手动添加其他路由(见下文)。

入门

安装此软件包最简单的方法是使用终端中的composer

composer require moxie-lean/wp-endpoints-routes

或者在您的`composer.json`文件中添加以下行

"require": {
  "moxie-lean/wp-endpoints-routes": "dev-master"
}

这将从packagist网站下载文件,并在存储库的master分支上设置最新版本。

之后,您可以通过包含`autoload.php`文件来在对象创建期间自动加载类。

include '/vendor/autoload.php';

最后,您需要初始化端点,将以下代码添加到您的代码中

\Lean\Endpoints\Routes::init();

用法

端点不接受输入,并以下列格式返回数据

[
  {
    "state": "home",
    "url": "/",
    "template": "home",
    "endpoint": "post",
    "params": {
      "id": 123
    }
  },
  {
    "state": "allPhotos",
    "url": "/photos",
    "template": "allPhotos",
    "endpoint": "collection",
    "params": {
      "type": "photo",
      "posts_per_page": 10
    }
  },
  {
    "state": "authorPhotos",
    "url": "/photos/:authorId",
    "template": "authorPhotos",
    "endpoint": "collection",
    "params": {
      "type": "photo",
      "posts_per_page": 10
    }
  },
  {
    "state": "photo",
    "url": "/photos/:authorId/:photoId",
    "template": "photo",
    "endpoint": "post",
    "params": {}
  }
]

添加额外路由

您可以使用以下方式使用`ln_endpoints_data_routes`过滤器执行此操作

add_filter( 'ln_endpoints_data_routes', 'add_extra_routes' );

function add_extra_routes( $routes ) {
    $extra_routes = [
        [
            'state' => 'blog',
            'url' => '/blog/',
            'template' => 'blog',
            'endpoint' => 'collection',
            'params' => [
                'type' => 'post',
                'posts_per_page' => 5,
            ]
        ],
    ];

    return array_merge( $routes, $extra_routes );
}