How to build a REST API with Laravel Orion

January 05, 2022

Tags: Technologies

laravel

 

An Application Programming Interface, or API for its acronym in English, is one of the most complicated concepts in the world of programming, but also the one that has been the most useful in recent years since they are a series of protocols that enable the design and integration of software and applications.

 

Mulesoft's official page defines an API as "API is the acronym for Application Programming Interface, which is a software intermediary that allows two applications to communicate with each other. Every time you use an application like Facebook, it sends an instant message. or check the weather on your phone, you are using an API. "

 

CodeAcademy defines REST as "REST, or Representational State Transfer is an architectural style for providing standards between computer systems on the web, facilitating communication between systems. REST-compliant systems, often called RESTful systems, are characterized by the way they are a stateless and separate client and server concerns.

 

Now let's see how to build a REST API with the help of Laravel Orion

 

Laravel Orion allows you to create REST APIs in a matter of minutes, working with removable soft models and doing an exhaustive search.

 

Model resources

 

Let's assume you have a 'Post' model that represents a blog post and you want to handle it via a Rest API. With Laravel Orion, this can be accomplished in three simple steps.

 

First, we are going to create the 'PostController' and extend it from 'OrionHttpControllersController'

 

<? php
 
namespace App \ Http \ Controllers \ Api;
 
use App \ Models \ Post;
use Orion \ Http \ Controllers \ Controller;
 
class PostsController extends Controller
{
 
}

Then define the '$ model' property and set it to the fully qualified model class name. The full controller should look like this:

 

<? php
 
namespace App \ Http \ Controllers \ Api;
 
use Orion \ Http \ Controllers \ Controller;
 
class PostsController extends Controller
{
    / **
     * Fully-qualified model class name
     * /
    protected $ model = Post :: class; // or "AppModelsPost"
}

 

Finally, register the routes in 'api.php' and call 'Orion::resource'

 

<? php
 
use Illuminate \ Support \ Facades \ Route;
use Orion \ Facades \ Orion;
use App \ Http \ Controllers \ PostsController;
 
Route :: group (['as' => 'api.'], Function () {
    Orion :: resource ('posts', PostsController :: class);
});

 

Done: tada:, You can now create, list, search, view, update, and delete blog posts via REST API. Try creating a post via ((POST) https: // <your app url> / api / posts endpoint: wink:

 

You can also see all available endpoints by running the php artisan route: list command.

 

So we can create a REST API with Laravel Orion and that works for multiple purposes, like the one we saw in this example of handling the publication of a blog. If you want to be one of the developers that creates cutting-edge technologies like this, just click here and you can be part of a multicultural team at Rootstack.

 

We recommend you on video


 

Let's work together!