dimvic/yiiyin

此包已被废弃,不再维护。未建议替代包。

Yii 1.1 json:api 模块

维护者

详细信息

github.com/dimvic/yii-yin

源代码

问题

安装: 43

依赖: 0

建议者: 0

安全: 0

星标: 2

关注者: 1

分支: 0

开放问题: 0

类型:yii-module

dev-master 2020-05-10 21:33 UTC

This package is not auto-updated.

Last update: 2021-09-04 02:23:23 UTC


README

Packagist package License

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(使用行为)
  • 允许使用自定义的仓库、转换器和填充器