A really simple package that provides a CRUD JSON API for your Laravel 5 application.
- Live Demo.
- Laravel 5.2 Example Package.
- Plays well with jQuery CRUDdy.
composer require askedio/laravel-cruddy:dev-master
Add the Service Provider to your providers array.
'providers' => [
Askedio\Laravel5ApiController\Providers\GenericServiceProvider::class,
...
Add the traits to your Model to enable the Api and Search features. More Details & Options.
class User extends Authenticatable
{
use \Askedio\Laravel5ApiController\Traits\ModelTrait;
use \Askedio\Laravel5ApiController\Traits\SearchableTrait;
...
Create a new controller for your API. More Details & Options.
<?php
namespace App\Http\Controllers\Api;
use Illuminate\Routing\Controller as BaseController;
class UserController extends BaseController
{
use \Askedio\Laravel5ApiController\Traits\ControllerTrait;
public $modal = \App\User::class;
}
Create a prefixed group for your api and assign the api and jsonapi middleware. More Details & Options.
Route::group(['prefix' => 'api', 'middleware' => ['api', 'jsonapi']], function()
{
Route::resource('user', 'Api\UserController');
});
Consume the API using Laravels resource routes, GET, PATCH, POST and DELETE. More Details & Options.
GET /api/user/1
HTTP/1.1 200 OK
Content-Type: application/vnd.api+json
{
"data": {
"type": "users",
"id": 1,
"attributes": {
"id": 1,
"name": "Test User",
"email": "[email protected]"
}
},
"links": {
"self": "/api/user/1"
},
"jsonapi": {
"version": "1.0",
"self": "v1"
}
}
My goal is a plug-n-play json api for Laravel. You shouldn't need to configure much of anything to enable the api on your models but if you still want advanced features like relations, searching, etc, you get that too.
If you have any comments, opinions or can code review please reach me here or on twitter, @asked_io. You can also follow me on my website, asked.io.
Thank you.
-William