Orion for Laravel
Orion for Laravel
The simplest way to create REST API with Laravel
Terminalcomposer require tailflow/laravel-orion
Simple yet powerful
Fully featured REST API for your Eloquent models and relationships with the simplicity of Laravel as you love it.
Easy to use and learn
Utilizes standard Laravel features such as Request classes, Policies and API Resources.
SDK and OpenAPI specs
TypesScript SDK and OpenAPI specifications out of the box.
First, define controllers
Later, you would setup operation hooks, filterable and searchable attributes, includable relationships, and more on the controllers.
<?php namespace App\Http\Controllers\Api; use App\Models\Post; use Orion\Http\Controllers\Controller; class PostsController extends Controller { /** * Fully-qualified model class name */ protected $model = Post::class; // or "App\Models\Post" }
Then, register routes
Orion provides a way to register entire model or relationship API resources with a single line of code.
api.php<?php use Illuminate\Support\Facades\Route; use Orion\Facades\Orion; use App\Http\Controllers\PostsController; use App\Http\Controllers\PostTagsController; Route::group(['as' => 'api.'], function() { Orion::resource('posts', PostsController::class)->withSoftDeletes(); Orion::morphToManyResource('posts', 'tags', PostTagsController::class); });
Finally, enjoy a fully featured REST API
You can now manage posts and their tags via a standardized API.
Make sure you have a policy for the model you are exposing via the API.
Alternatively, for local testing, you can consider using the
DisableAuthorization
trait to avoid getting a 403 error if the policy is not registered or is incorrect.+-----------+------------------------------+-----------------------------+------------------------------------------------------------+------------+ | Method | URI | Name | Action | Middleware | +-----------+------------------------------+-----------------------------+------------------------------------------------------------+------------+ | GET|HEAD | api/posts | api.posts.index | App\Http\Controllers\Api\PostsController@index | api | | POST | api/posts/search | api.posts.search | App\Http\Controllers\Api\PostsController@index | api | | POST | api/posts | api.posts.store | App\Http\Controllers\Api\PostsController@store | api | | GET|HEAD | api/posts/{post} | api.posts.show | App\Http\Controllers\Api\PostsController@show | api | | PUT|PATCH | api/posts/{post} | api.posts.update | App\Http\Controllers\Api\PostsController@update | api | | DELETE | api/posts/{post} | api.posts.destroy | App\Http\Controllers\Api\PostsController@destroy | api | | POST | api/posts/{post}/restore | api.posts.restore | App\Http\Controllers\Api\PostsController@restore | api | | POST | api/posts/batch | api.posts.batchStore | App\Http\Controllers\Api\PostsController@batchStore | api | | PATCH | api/posts/batch | api.posts.batchUpdate | App\Http\Controllers\Api\PostsController@batchUpdate | api | | DELETE | api/posts/batch | api.posts.batchDestroy | App\Http\Controllers\Api\PostsController@batchDestroy | api | | POST | api/posts/batch/restore | api.posts.batchRestore | App\Http\Controllers\Api\PostsController@batchRestore | api |
Generate OpenAPI specifications
To automatically generate OpenAPI specifications, simply run one command.
Terminalphp artisan orion:specs