moxie-leean / wp-endpoints-routes
0.1.1
2016-04-05 00:26 UTC
Requires
- php: >=5.4
- moxie-leean/wp-endpoint: 0.*.*
Requires (Dev)
This package is auto-updated.
Last update: 2022-02-01 12:57:03 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';
最后,您需要通过在代码中添加以下内容来初始化端点
\Leean\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 );
}