dimvic / yiiyin
此包已被废弃,不再维护。未建议替代包。
Yii 1.1 json:api 模块
dev-master
2020-05-10 21:33 UTC
Requires
- php: ^5.6.0|^7.1
- dimvic/composer-installers: dev-master
- justinrainbow/json-schema: 1.5.*
- psr/http-message: ^1.0.0
- seld/jsonlint: 1.3.*
- woohoolabs/yin: ^0.10.2
- zendframework/zend-diactoros: ^1.1.0
Suggests
- yiisoft/yii: ^1.1.17
This package is not auto-updated.
Last update: 2021-09-04 02:23:23 UTC
README
Yii 1.1 模块,直接添加并配置,可以自动通过兼容 json:api 1.0 的网络服务暴露资源(CActiveRecord 模型)。
感谢 yin 提供了一个出色的库,并为此模块提供了一个基于的示例。
感谢 Máté Kocsis 提供帮助并合并了拉取请求。
支持的功能
- GET /{type}/{id}
- GET /{type}/{id}/relationships/{relationship}
- GET /{type}/{id}/{relationship}
- PATCH /{type}/{id}
- POST /{type}
- DELETE /{type}/{id}
用法
只需配置模块,您就拥有了一个功能齐全的 HATEOAS 网络服务,用于您的模型。
我相信配置是自我解释的,您需要注意的唯一一点是,您需要为每个暴露的关系类型配置一个类型(即使有一个活动的方法数组)。
return [ ... 'modules' => [ 'yiiyin' => [ 'route' => 'api',//expose the module at /api 'controllerMap' => [//only add this if you want all requests logged 'default'=> [ 'class'=>'dimvic\\YiiYin\\ApiLogController',//log using Yii::log($log, 'info', 'json:api') ], ], 'resources' => [ 'Book' => [//exposed model 'type' => 'books',//exposed at api/books 'methods' => ['GET', 'POST', 'PATCH', 'DELETE'],//API methods supported for this model 'exposedRelationships' => [//all relations a client may access using the API 'book_i18ns' => 'book_i18ns',//relation name => API type (route) 'authors' => 'authors', 'publisher' => 'publishers', ], 'defaultRelationships' => [//all relations a client may access using the API 'book_i18ns' => 'book_i18ns',//relation name => API type (route) 'authors' => 'authors', 'publisher' => 'publishers', ], ], 'BookI18n' => [ 'type' => 'book_i18ns', 'methods' => ['GET', 'POST', 'PATCH'], ], 'Author' => [ 'type' => 'authors', 'methods' => ['GET', 'POST', 'PATCH'], ], 'Publisher' => [ 'type' => 'publishers', 'methods' => ['GET', 'POST', 'PATCH'], 'exposedRelationships' => ['representatives' => 'representatives'], 'defaultRelationships' => ['representatives' => 'representatives'], ], 'Representative' => [ 'type' => 'representatives', 'methods' => ['GET', 'POST', 'PATCH'], ], ], ], ... ], ... 'components' => [ 'urlManager' => [ 'urlFormat' => 'path', 'showScriptName' => false, 'rules' => [ ['class' => 'dimvic\\YiiYin\\ApiUrlRule'], ... ], ], ], .... ];
演示
示例项目可以在 这里 找到。只需不到一分钟即可设置。
待办事项
- 修复
PATCH {"relationship": {"data":null}}
- GET
/{resource}
分页 - GET
?include
& 包含关系的预加载 - GET
?filter
- 审查错误代码 & 消息
- 控制器过滤器以验证请求(请参阅 yin-middlewares)
- 为暴露的模型生成 UUID(使用行为)
- 允许使用自定义的仓库、转换器和填充器