Laravel Repository Controller
Easily open an HTTP api over your Laravel database., (*1)
Setup
Install with composer:, (*2)
composer require foothing/laravel-repository-controller
, (*3)
This package will define several routes in a REST-like format
that will perform operations on your database., (*4)
Add the service provider in config/app.php
:, (*5)
"providers" => [
Foothing\RepositoryController\RepositoryControllerServiceProvider::class,
],
In order to enable the routes, you'll need to declare them in
your routes.php
:, (*6)
// ... your app routes
RouteInstaller::install('api/v1/');
Be careful and place the RouteInstaller::install()
method at the very
end of your routes.php
in order to avoid conflicts., (*7)
IMPORTANT: you will need to change the default Controllers namespace in RouteServiceProvider
like so:
protected $namespace = '';
, (*8)
Finally, configure your resources in the config file., (*9)
php artisan vendor:publish --provider="Foothing\RepositoryController\RepositoryControllerServiceProvider" --tag="config"
This will add the resources.php
file in your config
folder., (*10)
'resources' => array(
// Resources must be in the form 'resourceName' => 'resourceImplementation'
// The implementation should be a fully qualified namespace to the model.
'user' => 'App\User',
'foo' => 'My\Namespace\Foo',
),
This will enable the routes on the specified resources., (*11)
How to use
The RouteInstaller
will declare the package routes. You can specify
a prefix as an optional install()
argument. The process will enable
the following routes, which we'll describe in better details later., (*12)
VERB |
Url |
Notes |
GET |
[optionalPrefix]/resources/{resource}/{id?}/{args?} |
Read resources |
POST |
[optionalPrefix]/resources/{resource} |
Create resources |
PUT |
[optionalPrefix]/resources/{resource}/{id} |
Update resources |
DELETE |
[optionalPrefix]/resources/{resource}/{id} |
Delete resources |
PUT |
resources/{resource}/{id?}/link/{relation}/{related}/{relatedId} |
Attach many-to-many |
DELETE |
resources/{resource}/{id?}/link/{relation}/{related}/{relatedId} |
Detach many-to-many |
POST |
resources/bulk/{resource} |
Bulk create resources |
PUT |
resources/bulk/{resource} |
Bulk update resources |
Each api endpoint will return data in JSON
format., (*13)
Read resources
Verb |
Url |
Payload |
GET |
[optionalPrefix]/resources/{resource}/{id?}/{args?} |
none |
Examples, (*14)
-
GET api/v1/resources/user
: will return all users
-
GET api/v1/resources/user/15
: will return user with id = 15
-
GET api/v1/resources/user/15/roles
will return user 15 roles
Pagination, (*15)
This endpoint will handle 2 querystring args for pagination:
- page (pagination page)
- ipp (pagination items-per-page), (*16)
The result will be a Laravel paginated result like:, (*17)
{
"total":4,
"per_page":"25",
"current_page":1,
"last_page":1,
"next_page_url":null,
"prev_page_url":null,
"from":1,
"to":4,
"data":[ the resources array ]
}
Related resources, (*18)
You can pass an optional with
query string argument that will be used
to fetch relations within the requested resource:, (*19)
GET api/v1/resources/user/1?with=roles,posts
, (*20)
Auto eager loading relations, (*21)
Since this package relies on Laravel Repository you can take advantage
of that package eager loading features therefore enabling auto eager-load
features on each resource., (*22)
Create resources
Create the requested resource., (*23)
Verb |
Url |
Payload |
POST |
[optionalPrefix]/resources/{resource} |
{resourceData} |
Example, (*24)
POST api/v1/resources/user
, (*25)
POST payload, (*26)
{
name: 'foo',
email: 'foo@bar.baz'
}
HTTP Response, (*27)
{
id: 1,
name: 'foo',
email: 'foo@bar.baz'
}
Update resources
Update the requested resource., (*28)
Verb |
Url |
Payload |
PUT |
[optionalPrefix]/resources/{resource}/{id} |
{resourceData} |
Example, (*29)
PUT api/v1/resources/user/1
, (*30)
PUT payload, (*31)
{
id: 1,
name: 'updating name',
email: 'foo@bar.baz'
}
HTTP Response, (*32)
{
id: 1,
name: 'updating name',
email: 'foo@bar.baz'
}
Delete resources
Delete the requested resource., (*33)
Verb |
Url |
Payload |
DELETE |
[optionalPrefix]/resources/{resource}/{id} |
none |
Example, (*34)
DELETE api/v1/resources/user/1
, (*35)
Link, bulk create and bulk update
More info coming soon., (*36)
LICENSE
MIT, (*37)