ont / laravel-magic-rest
Universal REST contoller for CRUD operations with dynamic routes and support for file uploads.
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
Description
This laravel module generates on-the-fly routes for CRUD operations for defined models and relations. All generated routes are REST-like and also support some non-CRUD operations such as model field updating and file uploading.
Let us have this database structure:
<?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');
}
}
Then these routes will be generated automatically:
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"
Installation
Simply do:
composer require ont/laravel-magic-rest
and then add service provider to `
config/app.php`
<?php
'providers' => array(
...
'Ont\MagicRest\ServiceProvider',
...
),
?>
If you need change default route prefix `
/restto something else then
you must execute
php artisan vendor:publishand edit
config/magic-rest.php`
TODO
- change POST to PUT for field processor (it doesn't create anything)
- remove "value" from PUT request for relation processor
- update major version (backward incompatible changes)
- better documentation
- access permissions from laravel
- add docs about file uploader optional dependency