Author - StudySection Post Views - 334 views
orion

Build REST APIs using Laravel Orion

In Laravel to create APIs, we keep writing the same code again and again. Firstly we create a model, then a controller and then create/update/delete/get methods for the model. These steps are repeated again and again whenever we create a new model.
Laravel Orion allows us to build fully-featured REST APIs in a matter of time by providing common endpoints for CRUD operations, soft deletable models, and a comprehensive search. It works with Laravel solutions like Requests for handling validation, Policies for handling authorization, and Resources for transforming responses. And it also works for complex relations like “belongsToMany” and “morphToMany”.

  1. Install Laravel Orion in your existing laravel project by using the below command:
    composer require tailflow/laravel-orion
  2. Let’s assume we have a model User and we want to build APIs for it.
  3. Create “UsersController” and extend it from “Orion\Http\Controllers\Controller”
    <?php
    namespace App\Http\Controllers;
    use App\Models\User;
    use Orion\Http\Controllers\Controller;
    class UsersController extends Controller
    {
    }
  4. Then define the “protected $model” property and set it to the User model class name. The controller should look like below
    <?php
    namespace App\Http\Controllers;
    use Orion\Http\Controllers\Controller;
    use App\Models\User;
    class UsersController extends Controller
    {
    /**
    * Fully-qualified model class name
    */
    protected $model = User::class; // or "App\Models\User"
    }
  5. Now, register the routes in api.php by pasting the following code.
    <?php
    use Illuminate\Support\Facades\Route;
    use Orion\Facades\Orion;
    use App\Http\Controllers\UsersController;
    Route::group(['as' => 'api.'], function() {
    Orion::resource('users', ‘UsersController’);
    });
  6. Now you can perform create/list/search/view/update/delete users via REST API. Try to create a user via (POST) https:///api/users endpoint
  7. To list all available routes, you can run below artisan command.
    php artisan route:list
    You will get all registered routes as shown below.

    data

Note: Make sure to have policy created and registered for the model you are exposing via the API or consider using DisableAuthorization trait (only for local testing) to avoid getting 403 error, if the policy is not registered or incorrect.

StudySection gives an opportunity to beginners and experts in .NET framework to go through StudySection’s .NET Certification Exam and get a .NET certification for enhancement of career in programming. If you have knowledge of the .NET framework then you can get a certificate through an online exam at StudySection.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri