Download the PHP package nilportugues/laravel5-json-api-dingo without Composer

On this page you can find all versions of the php package nilportugues/laravel5-json-api-dingo. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel5-json-api-dingo

Laravel 5 JSON API Mappings with Dingo

Scrutinizer Code Quality SensioLabsInsight Latest Stable Version Total Downloads License Donate

This package makes Laravel 5 JSON API Server Package and Dingo play well together by using Dingo's API routing system instead of Laravel's.

By doing this you get the automatic formatting of the JSON API mappings and the versioning and security offered by Dingo.

Features

Installation

Use Composer to install the package:

Now run the following artisan command:

Configuration

Open up config/app.php and add the following line under providers array:

Usage

Defining routes

We will be planning the resources ahead its implementation. All routes require to have a name.

This is how our app/Http/routes.php will look:

Definition

First, let's define the Models for Employees and Orders using Eloquent.

Employees (Eloquent Model)

Employees SQL

Orders (Eloquent Model)

Orders SQL

Follow up, we'll be creating Transformers. One Transformer is required for each class and it must implement the \NilPortugues\Api\Mappings\JsonApiMapping interface.

We will be placing these files at app/Model/Api:

EmployeesTransformer

Same goes for Orders, these files will also be placed at app/Model/Api:

OrdersTransformer

Usage

Open config/jsonapi.php. This file should return an array returning all the class mappings.

JsonApiController

Whether it's Laravel 5 or Lumen, usage is exactly the same.

Let's create a new controller that extends the JsonApiController provided by this package, as follows:

Lumen users must extends from LumenJsonApiController not JsonApiController.

In case you need to overwrite any default behaviour, the JsonApiController methods are:

But wait! We're missing out one action, EmployeesController@getOrdersByEmployee.

As the name suggests, it should list orders, so the behaviour should be the same as the one of ListAction.

If you look inside the listActionyou'll find a code similar to the one below, but we just ajusted the behaviour and used it in our controller to support an additional action:

And you're ready to go. Yes, it is THAT simple!

Examples: Consuming the API

GET

This is the output for EmployeesController@getAction being consumed from command-line method issuing: curl -X GET "http://localhost:9000/employees/1".

Output:

POST

POST requires all member attributes to be accepted, even those hidden by the mapper.

For instance, attachments member was hidden, but it is required, so it needs to be passed in with a valid value. On the other hand, full_name member value must not be passed in as an attribute or resource creation will fail.

Passing and id is optional and will be used instead of a server-side generated value if provided.

Sending the following data to the server using POSTto the following URI http://localhost:9000/employees:

Will produce:

Notice how 201 HTTP Status Code is returned and Location header too. Also attachments is not there anymore, and full_name was displayed.

PUT

PUT requires all member attributes to be accepted, just like POST.

For the sake of this example, we'll just send in a new job_title value, and keep everything else exactly the same.

It's important to notice this time we are required to pass in the id, even if it has been passed in by the URI, and of course the id values must match. Otherwise it will fail.

Sending the following data to the server using PUTto the following URI http://localhost:9000/employees/10:

Will produce:

PATCH

PATCH allows partial updates, unlike PUT.

We are required to pass in the id member, even if it has been passed in by the URI, and of course the id values must match. Otherwise it will fail.

For instance, sending the following data to the server using the following URI http://localhost:9000/employees/10:

Will produce:

DELETE

DELETE is the easiest method to use, as it does not require body. Just issue a DELETE to http://localhost:9000/employees/10/ and Employee with id 10 will be gone.

It will produce the following output:

And notice how response will be empty:


GET Query Params: include, fields, sort and page

According to the standard, for GET method, it is possible to:

For instance, passing /employees/10?fields[employee]=company,first_name will produce the following output:

For instance, /employees?include=order will only load order type data inside include member, but /employees?include=order.employee will only load those orders related to the employee type.

For instance: /employees?sort=surname,-first_name

For instance: /employees?page[number]=1&page[size]=10

POST/PUT/PATCH with Relationships

The JSON API allows resource creation and modification and passing in relationships that will create or alter existing resources too.

Let's say we want to create a new Employee and pass in its first Ordertoo.

This could be done issuing 2 POST to the end-points (one for Employee, one for Order) or pass in the first Order as a relationship with our Employee, for instance:

Due to the existance of this use case, we'll have to ajust our Controller implementation overwriting some methods provided by the JsonApiController: createResourceCallable, updateResourceCallable and patchResourceCallable.

Here's how it would be done for createResourceCallable.

It is important, in order to use Transactions, do define in Eloquent models the $fillable values.

Here's how Employees and Orders look like with $fillable defined.

Employees (Eloquent Model) with $fillable

Orders (Eloquent Model) with $fillable

Custom Response Headers

Adding custom response headers can be done for multiple reasons: versioning, setting expire headers, caching, setting private or public the served content...

In order to do this, it's as simple as overwriting the JsonApiController addHeaders method. For instance, let's use the EmployeeController as an example:

Now all supported actions will include the added custom headers.

Common Errors and Solutions

"Undefined index: @type"

This usually happens because you did not write the namespace of your Mapping in config/jsonapi.php. Double check, if missing, add it and refresh the resource. It should be gone!

Contribute

Contributions to the package are always welcome!

Support

Get in touch with me using one of the following means:

Authors

License

The code base is licensed under the MIT license.


All versions of laravel5-json-api-dingo with dependencies

PHP Build Version
Package Version
Requires nilportugues/laravel5-json-api Version ^2.4
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package nilportugues/laravel5-json-api-dingo contains the following files

Loading the files please wait ....