Package Data | |
---|---|
Maintainer Username: | brazorf |
Maintainer Contact: | fabrizio.ranieri@gmail.com (brazorf) |
Package Create Date: | 2016-07-05 |
Package Last Update: | 2021-06-30 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-10-26 15:04:08 |
Package Statistics | |
---|---|
Total Downloads: | 131 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 2 |
Total Watchers: | 3 |
Total Forks: | 1 |
Total Open Issues: | 0 |
Easily open an HTTP api over your Laravel database.
Install with composer:
composer require foothing/laravel-repository-controller
This package will define several routes in a REST-like format that will perform operations on your database.
Add the service provider in config/app.php
:
"providers" => [
Foothing\RepositoryController\RepositoryControllerServiceProvider::class,
],
In order to enable the routes, you'll need to declare them in
your routes.php
:
// ... your app routes
RouteInstaller::install('api/v1/');
Be careful and place the
RouteInstaller::install()
method at the very end of yourroutes.php
in order to avoid conflicts.
Finally, configure your resources in the config file.
php artisan vendor:publish --provider="Foothing\RepositoryController\RepositoryControllerServiceProvider" --tag="config"
This will add the resources.php
file in your config
folder.
'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.
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.
|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.
|Verb|Url|Payload|
|----|---|-------|
|GET |[optionalPrefix]/resources/{resource}/{id?}/{args?}
| none|
Examples
GET api/v1/resources/user
: will return all usersGET api/v1/resources/user/15
: will return user with id = 15GET api/v1/resources/user/15/roles
will return user 15 rolesPagination
This endpoint will handle 2 querystring args for pagination:
The result will be a Laravel paginated result like:
{
"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
You can pass an optional with
query string argument that will be used
to fetch relations within the requested resource:
GET api/v1/resources/user/1?with=roles,posts
Auto eager loading relations
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.
Create the requested resource.
|Verb|Url|Payload|
|----|---|-------|
|POST |[optionalPrefix]/resources/{resource}
| {resourceData}|
Example
POST api/v1/resources/user
POST payload
{
name: 'foo',
email: 'foo@bar.baz'
}
HTTP Response
{
id: 1,
name: 'foo',
email: 'foo@bar.baz'
}
Update the requested resource.
|Verb|Url|Payload|
|----|---|-------|
|PUT |[optionalPrefix]/resources/{resource}/{id}
| {resourceData}|
Example
PUT api/v1/resources/user/1
PUT payload
{
id: 1,
name: 'updating name',
email: 'foo@bar.baz'
}
HTTP Response
{
id: 1,
name: 'updating name',
email: 'foo@bar.baz'
}
Delete the requested resource.
|Verb|Url|Payload|
|----|---|-------|
|DELETE |[optionalPrefix]/resources/{resource}/{id}
| none|
Example
DELETE api/v1/resources/user/1
More info coming soon.
MIT