Download the PHP package jarischaefer/hal-api without Composer
On this page you can find all versions of the php package jarischaefer/hal-api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package hal-api
HAL-API
Enhances your HATEOAS experience by automating common tasks.
About
This package is based on Laravel 5. It is designed to automate common tasks in RESTful API programming. These docs might not always be in sync with all the changes.
Installation
Requirements
Requires Laravel 5.4 and PHP 7.1.
Composer
Either require the package via Composer by issuing the following command
composer require jarischaefer/hal-api:dev-master
or by including the following in your composer.json.
Check the releases page for a list of available versions.
Service Provider
app.php
Register the Service Provider in your config/app.php file.
compile.php (optional step)
Register the Service Provider in your config/compile.php file.
Run php artisan optimize --force
to compile an optimized classloader.
Usage
Simple Controller
This type of controller is not backed by a model and provides no CRUD operations. A typical use case is an entry point for the API. The following controller should be routed to the root of the API and lists all relationships.
Resource Controller
Resource controllers require three additional components:
- Model: Resources' data is contained within models
- Repository: Repositories retrieve and store models
- Transformer: Transforms models into HAL representations
Models
The following is a simple relationship with two tables. User has a One-To-Many relationship with Post.
Repository
You may create an Eloquent-compatible
repository by extending HalApiEloquentRepository
and implementing its getModelClass() method.
Searchable repository
Implementing HalApiSearchRepository enables searching/filtering by field. An Eloquent-compatible repository is available. Not restricting the searchable fields might result in information leakage.
Transformer
Transformers provide an additional layer between your models and the controller. They help you create a HAL response for either a single item or a collection of items.
Linking relationships
Overriding a transformer's getLinks method allows you to link to related resources. Linking a Post to its User:
Notice the "users.show" relation among the links.
Embedded relationships
Once data from two separate Models needs to be combined, the linking-approach doesn't quite cut it. Displaying Posts' authors (firstname and lastname in User model) becomes infeasible with more than a dozen items (N+1 GET requests to all "users.show" relationships). Embedding related data is basically the same as eager loading.
Notice the "users.show" relation in the _embedded
field.
Dependency wiring
It is recommended that you wire the transformers' dependencies in a Service Provider:
routes.php
The RouteHelper automatically creates routes for all CRUD operations.
Disabling CRUD operations and pagination
Searching/filtering
The controller's repository must implement HalApiSearchRepository.
RouteServiceProvider
Make sure you bind all route parameters in the RouteServiceProvider. The callback shown below handles missing parameters depending on the request method. For instance, a GET request for a nonexistent database record should yield a 404 response. The same is true for all other HTTP methods except for PUT. PUT simply creates the resource if it did not exist before.
Exception handler
The callback above throws NotFoundHttpException if no record was found. To create a proper response instead of an error page, the exception handler must be amended. As shown below, various HTTP status codes like 404 and 422 will be returned depending on the exception caught.
Examples
JSON for a specific model (show)
JSON for a list of models (index)
Contributing
Feel free to contribute anytime. Take a look at the Laravel Docs regarding package development first. Once you've made some changes, push them to a new branch and start a pull request.
License
This project is open-sourced software licensed under the MIT license.