This package is still in development !
- PHP 7.3+
- PHPUnit 8.0+
- Laravel 7.0+
To install through composer, simply put the following in your composer.json
file:
{
"require-dev": {
"vgirol/jsonapi": "dev-master"
}
}
And then run composer install
from the terminal.
Above installation can also be simplified by using the following command:
composer require vgirol/jsonapi
The package will automatically register itself.
If you're not using Package Discovery, add the Service Provider to your config/app.php file:
VGirol\JsonApi\JsonApiServiceProvider::class
You have to publish the 2 config files with:
php artisan vendor:publish --provider="VGirol\JsonApi\JsonApiServiceProvider" --tag="config"
Create Model, migration and seed.
// app/Models/Company.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Company extends Model
{
protected $table = 'xxx';
protected $primaryKey = 'xxx';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'xxx'
];
public function establishments()
{
return $this->hasMany('App\Models\Establishment', $this->getKeyName());
}
}
For each model, create a form request extending VGirol\JsonApi\Requests\ResourceFormRequest
abstract class.
// app/Http/Request/CompanyFormRequest.php
namespace App\Http\Requests;
use VGirol\JsonApi\Requests\ResourceFormRequest;
class CompanyFormRequest extends ResourceFormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
return [
//
];
}
}
Create route using Route::jsonApiResource
macro.
// routes/api.php
Route::jsonApiResource(
'companies', // Route name
);
Publish the config files (jsonapi-alias.php
and jsonapi.php
).
php artisan vendor:publish --provider="VGirol\JsonApi\JsonApiServiceProvider" --tag="config"
Fill the jsonapi-alias.php
config file.
// jsonapi-alias.php
return [
'groups' => [
[
'type' => 'company', // Resource type
'route' => 'companies', // Route name
'model' => \App\Models\Company::class,
'request' => \App\Http\Requests\CompanyFormRequest::class
],
[
//
]
]
];
That's all !
A user guide can be found here.
The API documentation is available in XHTML format at the url http://jsonapi.girol.fr/docs/ref/index.html.
Please see CHANGELOG for more information on what has changed recently.
composer test
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
If you discover any security related issues, please email vincent@girol.fr instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.