ont / laravel-magic-rest
通用的REST控制器,支持CRUD操作、动态路由以及文件上传功能。
0.0.6-alpha
2015-12-04 05:27 UTC
Requires
- php: >=5.4.0
- illuminate/config: ~5.0
- illuminate/support: ~5.0
- ont/laravel-interfaces: dev-default
Requires (Dev)
- phpunit/phpunit: ~4.0
This package is auto-updated.
Last update: 2020-07-28 17:13:28 UTC
README
描述
此Laravel模块可以为定义的模型和关系动态生成CRUD操作的路径。所有生成的路径都类似于REST,并且还支持一些非CRUD操作,例如更新模型字段和文件上传。
让我们看看这个数据库结构
<?php
class Author extends Model
{
public function books() {
return $this->belongsToMany('App\Book');
}
}
class Book extends Model
{
public function author() {
return $this->belongsToMany('App\Author');
}
}
然后这些路径将自动生成
GET /rest/book get list of App\Books models
POST /rest/book create new book
GET /rest/book/[id] returns single book
PUT /rest/book/[id] updates book from json
DELETE /rest/book/[id] delete book
GET /rest/book/[id]/authors get list of authors
GET /rest/book/[id]/authors/[id] get single author
POST /rest/book/[id]/relation create new related model from json and
attach it to book with [id]. Returns
json of created model.
PUT /rest/book/[id]/authors updates authors from json field "value"
For example you can POST this json:
{ value: [
{ 'id': 123, 'name': 'James' },
{ 'name': 'John' },
]}
After this magic rest will create new author with name 'John', will update
old author's name with 'id' 123 and remove all another authors from relation.
Returns json of updated relation.
For one-to-one relation you must send object {value: {id:123}}
For one-to-many and many-to-many you must send list {value: [{id:1}, {id:2},...]}
GET /rest/book/[id]/description returns value of field "description"
DELETE /rest/book/[id]/another_field removes uploaded file / empty field
POST /rest/book/[id]/field can update model "field" from different sources:
1) directly from POST json "value" field
2) upload file from POST "file" field and save path into "field"
2) upload file from POST json "base64" field (file content in base64)
and "name" json field (original filename) and save uploaded path to "field"
安装
只需这样做
composer require ont/laravel-magic-rest
然后将服务提供者添加到 `
config/app.php`
<?php
'providers' => array(
...
'Ont\MagicRest\ServiceProvider',
...
),
?>
如果您需要将默认的路由前缀 `
/rest更改为其它的前缀,则必须执行
php artisan vendor:publish并编辑
config/magic-rest.php`
待办事项
- 将字段处理程序的POST更改PUT(它不会创建任何内容)
- 从关系处理程序的PUT请求中移除"值"
- 更新主版本(向后不兼容的更改)
- 更好的文档
- 从Laravel获取访问权限
- 添加有关文件上传可选依赖的文档