Download the PHP package tomhart/laravel-restful-controller without Composer
On this page you can find all versions of the php package tomhart/laravel-restful-controller. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tomhart/laravel-restful-controller
More information about tomhart/laravel-restful-controller
Files in tomhart/laravel-restful-controller
Package laravel-restful-controller
Short Description Laravel Restful Controller
License MIT
Homepage https://github.com/
Informations about the package laravel-restful-controller
Laravel Restful Controller
This library adds an AbstractRestfulController
to to be basic heavy lifting of a CRUD controller.
- Installation
- Usage
- Relationships
- Loading Relationships
- Accessing Relationships
- Restricting Access to Models
- Index page
- Show, Update, and Destroy Pages
- Manipulating models before saving or updating
- Pagination
- Filtering
- HasLinks
- Builder
Installation
You can install this package via composer using this command:
composer require tomhart/laravel-restful-controller
Usage
-
Create a controller extending from this, and implement the method
getModelClass
- If you want it to render views for index, show, or store, add a
$views
property
If $views
is empty, the specified view doesn't exist, or the Accept
header is application/json
, then JSON is returned
- Define a resource route
Note this also would define a blogs.show.extra
, and blogs.show.options
route
which will be explained later.
Example response for: /blogs/1
Relationships
Loading Relationships
The show route can return your models relationships. If you send a X-Load-Relationship
header,
with a comma separated value list of headers to load. See the testRelationshipsCanBeReturned
test
for an example.
Example response for: /blogs/1
with X-Load-Relationship: comments
Accessing Relationships
You can drill into a relationship using the .show.extra
route mentioned above. If the first comment
had an author
and you wanted to see, via the blog resources, you can call /blogs/1/comments[0]/author
You can dynamically build the route using
Restricting Access to Models
You'll most likely want to restrict access to certain models, e.g. only load the logged in users posts. To do that, there's a few methods you can overwrite.
Index Page
In order to restrict the models returned by the index route, e.g. a paginated list of many models,
overwrite the createModelQueryBuilder
method.
Show, Update, and Destroy Pages
In order to restrict which indiviual models can be shown, updated, or deleted, overwrite the
findModel
method.
Manipulating models before saving or updating
If you want to manipulate the model before they are saved, or updated, e.g. setting the user_id
to the current logged in user, override the saveModel
method.
Pagination
By default the index
route, and any relationships it's trying to load will be paginated if possible.
Example response for: /blogs
Filtering
You can filter the index
route via a query string, e.g. ?name=test
.
HasLinks
This library also provides HasLinks
interface, and a HasLinksTrait
to provide a default implementation. If you apply
those to your models, the responses will contain a _links
key to help your consumers navigate around and use your API.
Example _links
for /blogs/1
:
The .options
route mentioned earlier will simply return the index
, create
, and store
_links
for the resource
so you can query the endpoint and get the URLs needing to interfacing with the API.
If you send {"id": X}
, it'll also build the show
, update
, and delete
routes with the ID supplied.
Builder
This library also includes a Builder
class to interface with the API from a consumer view.
It supports the standard get
, insert
, update
, and delete
methods.
Example:
To use it with your model simply add implements Restful
, and use the trait InteractsWithRest
All versions of laravel-restful-controller with dependencies
illuminate/routing Version ~5.5.0|~5.6.0|~5.7.0|~5.8.0|~6.0|^7.0
illuminate/support Version ~5.5.0|~5.6.0|~5.7.0|~5.8.0|~6.0|^7.0
illuminate/database Version ~5.5.0|~5.6.0|~5.7.0|~5.8.0|~6.0|^7.0
guzzlehttp/guzzle Version ^6.5