[WordPress] 外掛分享: GP routing – WP compatible REST API

首頁外掛目錄 › GP routing – WP compatible REST API
全新外掛
安裝啟用
尚無評分
3765 天前
最後更新
問題解決
WordPress 3.0.1+ v1.0 上架:2015-11-26

內容簡介

這個外掛讓開發人員可以建立 REST-ish API,可以訪問 WordPress 和其所有外掛的物件。

使用方法

安裝此外掛就像安裝其他外掛一樣。
此項目分為兩組檔案:

* index.php - 這裡可以註冊路由和/或中介層(middleware)。
* routes/ - 這裡放置您的路由。

中介層(Middleware)

中介層是在每次呼叫 API 時執行的函式,通常用於驗證參數或

在路由到達之前轉換請求,中介層的代碼結構如下:

app::middleware(function () {
做一些事情();
});

中介層適用於每個動詞,不會自動分配 $_POST 或 $_GET 給任何變數

路由(Routes)

路由是您會連接請求到根據其 HTTP 動詞連接到一個函數的地方。
路由引擎將 /api/ 所有呼叫並將斜線附加到所有請求。
GET 請求會像這樣匹配:

app::get('/testget', function($req){
return $req;
});

其中:

* app::get - 這將數據匹配為 GET 請求。
* '/testget' - 路由引擎將會尋找的路徑。
* function(){ } ... - 當路由匹配時要執行的函數。
* $req - 發送到動詞的參數。

要對此路徑做出呼叫,完整路徑會是:

http://www.mysite.com/api/testget/

斜線後總是需要。
對於所有路由的參數都會作為回調的第一個參數發送。

應在 index 檔中註冊路由,方法是將 app::routes 與路由的檔案名 (不包括副檔名) 作為參數:

app::routes('test');

所有路由都會將其返回值作為 JSON 編碼回應輸出。

所有動詞的範例

GET:

app::get(‘/testget’, function($req){
return $req;
});

POST:

app::post(‘/testpost’, function($req){
return $req;
});

DELETE:

app::delete(‘/testdelete’, function($req){
return $req;
});

PUT:

app::put(‘/testput’, function($req){
return $req;
});

巢狀路由

路由可以像這樣被巢狀:

app::delete('/admin/machines', function($req){
return $req;
});

可透過向以下發出 DELETE 以呼叫:

http://www.mysite.com/api/admin/machines/<h3>路由和中介層的註冊</h3>
路由的代碼應該放在 "routes" 資料夾中的檔案中,然後像這樣在 index.php 檔案中 "註冊":

function gp_registerapimethods()
{
app::middleware(function () {

});

app::routes('test');

}

index.php 檔中只應存在 gp_registerapimethods 的一個實例。
中介層和路由可以有多個實例。
可以使用此調用而不是個別路由註冊以註冊路由文件夾中的所有路由:

app::routes('*');
<h3>所有範例</h3>index.php 檔案:

外掛標籤

開發者團隊

⬇ 下載最新版 (v1.0) 或搜尋安裝

① 下載 ZIP → 後台「外掛 › 安裝外掛 › 上傳外掛」
② 後台搜尋「GP routing – WP compatible REST API」→ 直接安裝(推薦)

原文外掛簡介

Allows developers to create REST-ish API’s that have access to all of WordPress and its plug-ins objects.
Usage
Install the plug-in as any other plug-in.
Project is divided in two sets of files:
* index.php - This is where you register routes and/or middleware.
* routes/ - This is where you place your routes.

Middleware
Middlewares are functions that will be executed on every call to the API, generally used to validate parameters or
transform requests before they reach routes, code for middleware is structured like this:
app::middleware(function () {
doSomething();
});

Middlewares run for every Verb and do not automatically assign $_POST or $_GET to any variable

Routes
A route is where you would link a request to a function depending on its HTTP Verb.
The routing engine will prepend /api/ to all calls and will append a trailing slash to all requests.
A GET request would be matched like this:
app::get('/testget', function($req){
return $req;
});

In which:
* app::get - This will match up a GET request.
* '/testget' - the path the routing engine will look for.
* function(){ } ... - the function to execute when the route is matched.
* $req - parameters sent to the verb.

To make a call to this route, the full path would be:
http://www.mysite.com/api/testget/

The trailing slash is always required.
Parameters for all routes will be sent as the first parameter to the callback.

A route should be registered on the index file by placing a call to app::routes with the filename of the route (without the extension) as a parameter:
app::routes('test');

All routes output their return value as a JSON encoded response.

Samples for all verbs

GET:
app::get(‘/testget’, function($req){
return $req;
});

POST:
app::post(‘/testpost’, function($req){
return $req;
});

DELETE:
app::delete(‘/testdelete’, function($req){
return $req;
});

PUT:
app::put(‘/testput’, function($req){
return $req;
});

Nested routes
Routes can be nested like this:
app::delete('/admin/machines', function($req){
return $req;
});

Which could be called by issuing a DELETE to:
http://www.mysite.com/api/admin/machines/

Route and middleware registration

Code for the routes should be placed on a file inside the "routes" folder and then "registered" on the index.php file like this:

function gp_registerapimethods()
{
app::middleware(function () {
});

app::routes('test');

}
Only one instance of the gp_registerapimethods should exist on the index.php file.
Middleware and Routes can have as many instances as needed.
All routes inside the routes folder can be registered by doing this call instead of the individual route registration:
app::routes('*');

Sample for everything

The index.php file has the "test" route registered, and that test.php route a call for each verb which you could use as a starting point for your own services.

延伸相關外掛

文章
Filter
Apply Filters
Mastodon