這篇文章主要介紹了深入淺析JSONAPI在PHP中的應用,需要的朋友可以參考下
現在服務端程式設計師的主要工作已經不再是套模版,而是編寫基於JSON 的API 介面。可惜大家寫介面的風格往往迥異,這就為系統整合帶來了很多不必要的溝通成本,如果你有類似的困擾,那麼不妨關註一下JSONAPI ,它是一個基於JSON 構建API 的規範標準,一個簡單的API 介面大致如下所示:
JSONAPI
#簡單說明一下:根節點中的data 用來放置主物件的內容,其中type 和id 是必須要有的字段,用來表示主對象的類型和標識,其它簡單的屬性統統放置到attributes 裡,如果主對象存在一對一、一對多等關聯對象,那麼放置到relationships 裡,不過只是通過type 和id 字段放置一個鏈接,關聯物件的實際內容統統放置在根接點中的included 裡。
有了 JSONAPI,資料解析的過程變得規範起來,節省了不必要的溝通成本。不過如果要手動建立JSONAPI 資料還是很麻煩的,好在透過使用Fractal 可以讓實現過程相對自動化一些,上面的例子如果用Fractal 實現大概是這個樣子:
<?php use League\Fractal\Manager; use League\Fractal\Resource\Collection; $articles = [ [ 'id' => 1, 'title' => 'JSON API paints my bikeshed!', 'body' => 'The shortest article. Ever.', 'author' => [ 'id' => 42, 'name' => 'John', ], ], ]; $manager = new Manager(); $resource = new Collection($articles, new ArticleTransformer()); $manager->parseIncludes('author'); $manager->createData($resource)->toArray(); ?>
如果讓我選最喜愛的PHP 工具包,Fractal 一定榜上有名,它隱藏了實作細節,讓使用者完全不必了解JSONAPI 協定即可上手。不過如果你想在自己的專案裡使用的話,與直接使用Fractal 相比,可以試試Fractalistic ,它對Fractal 進行了封裝,使其更好用:
<?php Fractal::create() ->collection($articles) ->transformWith(new ArticleTransformer()) ->includeAuthor() ->toArray(); ?>
如果你是裸寫PHP的話,那麼Fractalistic 基本上就是最佳選擇了,不過如果你使用了一些全棧框架的話,那麼Fractalistic 可能還不夠優雅,因為它無法和框架本身已有的功能更完美的融合,以Lavaral 為例,它本身內建了一個API Resources 功能,在此基礎上我實作了一個JsonApiSerializer,可以和框架完美融合,程式碼如下:
<?php namespace App\Http\Serializers; use Illuminate\Http\Resources\MissingValue; use Illuminate\Http\Resources\Json\Resource; use Illuminate\Http\Resources\Json\ResourceCollection; use Illuminate\Pagination\AbstractPaginator; class JsonApiSerializer implements \JsonSerializable { protected $resource; protected $resourceValue; protected $data = []; protected static $included = []; public function __construct($resource, $resourceValue) { $this->resource = $resource; $this->resourceValue = $resourceValue; } public function jsonSerialize() { foreach ($this->resourceValue as $key => $value) { if ($value instanceof Resource) { $this->serializeResource($key, $value); } else { $this->serializeNonResource($key, $value); } } if (!$this->isRootResource()) { return $this->data; } $result = [ 'data' => $this->data, ]; if (static::$included) { $result['included'] = static::$included; } if (!$this->resource->resource instanceof AbstractPaginator) { return $result; } $paginated = $this->resource->resource->toArray(); $result['links'] = $this->links($paginated); $result['meta'] = $this->meta($paginated); return $result; } protected function serializeResource($key, $value, $type = null) { if ($type === null) { $type = $key; } if ($value->resource instanceof MissingValue) { return; } if ($value instanceof ResourceCollection) { foreach ($value as $k => $v) { $this->serializeResource($k, $v, $type); } } elseif (is_string($type)) { $included = $value->resolve(); $data = [ 'type' => $included['type'], 'id' => $included['id'], ]; if (is_int($key)) { $this->data['relationships'][$type]['data'][] = $data; } else { $this->data['relationships'][$type]['data'] = $data; } static::$included[] = $included; } else { $this->data[] = $value->resolve(); } } protected function serializeNonResource($key, $value) { switch ($key) { case 'id': $value = (string)$value; case 'type': case 'links': $this->data[$key] = $value; break; default: $this->data['attributes'][$key] = $value; } } protected function links($paginated) { return [ 'first' => $paginated['first_page_url'] ?? null, 'last' => $paginated['last_page_url'] ?? null, 'prev' => $paginated['prev_page_url'] ?? null, 'next' => $paginated['next_page_url'] ?? null, ]; } protected function meta($paginated) { return [ 'current_page' => $paginated['current_page'] ?? null, 'from' => $paginated['from'] ?? null, 'last_page' => $paginated['last_page'] ?? null, 'per_page' => $paginated['per_page'] ?? null, 'to' => $paginated['to'] ?? null, 'total' => $paginated['total'] ?? null, ]; } protected function isRootResource() { return isset($this->resource->isRoot) && $this->resource->isRoot; } } ?>
對應的Resource 基本上還和之前一樣,只是回傳值改了一下:
<?php namespace App\Http\Resources; use App\Article; use Illuminate\Http\Resources\Json\Resource; use App\Http\Serializers\JsonApiSerializer; class ArticleResource extends Resource { public function toArray($request) { $value = [ 'type' => 'articles', 'id' => $this->id, 'name' => $this->name, 'author' => $this->whenLoaded('author'), ]; return new JsonApiSerializer($this, $value); } } ?>
對應的Controller 也和原來差不多,只是加入了一個isRoot 屬性,用來識別根:
<?php namespace App\Http\Controllers; use App\Article; use App\Http\Resources\ArticleResource; class ArticleController extends Controller { protected $article; public function __construct(Article $article) { $this->article = $article; } public function show($id) { $article = $this->article->with('author')->findOrFail($id); $resource = new ArticleResource($article); $resource->isRoot = true; return $resource; } } ?>
整個過程沒有對Laravel 的架構進行太大的侵入,可以說是目前Laravel 實現JSONAPI 的最優解決方案了,有興趣的可以研究一下JsonApiSerializer 的實現,雖然只有一百多行程式碼,但是我卻費了好大的力氣才實現,可以說是行行皆辛苦啊。
上面是我整理給大家的,希望今後對大家有幫助。
相關文章:
使用angular、react和vue如何實現相同的面試題元件
########################### ###在Angular2.0中如何實現modal對話框############在JS中如何實現運動緩衝效果(詳細教學)#######以上是在PHP中如何使用JSONAPI的詳細內容。更多資訊請關注PHP中文網其他相關文章!