Blog Detail

18

Feb
Make a Powerful Json API Compatible Rest API with Laravel cover image

arrow_back Make a Powerful Json API Compatible Rest API with Laravel

Laravel Restify is an extraordinary tool inspired by Laravel Nova for building robust, modern API. It is the fastest way to make a powerful JSON API compatible with Rest API with Laravel. If you already have an application built with Laravel Nova, you can transfer your data to Laravel Restify using basically the same resource classes content.

If you don’t have an application written with Nova, you can start with Laravel Restify from scratch and get a powerful API in a few minutes.

Features

  • CRUD over entities
  • Authentication with Sanctum
  • Handy Response maker
  • Powerful Search
  • JSON:API consistency
  • Customizable
  • Laravel Compatible Authorization

Requirements

Laravel Restify has a few requirements you should be aware of before installing it:

  • PHP >= 8.0
  • Laravel Framework >= 8.0

Installation

You can install this package via composer, Just run this command in the terminal:

composer require binaryk/laravel-restify

Setup

After the installation, the package requires a setup process:

php artisan restify:setup

The command above:

  • Publishes the config/restify.php configuration file
  • Creates the providers/RestifyServiceProvider and will add it in the config/app.php
  • Creates a new app/Restify directory
  • Creates an abstract app/Restif/Repository.php
  • Scaffolds a app/Restify/UserRepository repository for users CRUD

Quick start

Having the package setup and users table migrated, you should be good to perform the first API request:

GET: /api/restify/users?perPage=10&page=2

or using json api format:

GET: /api/restify/users?page[size]=10&page[number]=2

This should return the users list paginated and formatted according to JSON:API standard.

Configurations

Prefix

As you notice the default prefix for the restify API is /api/restify. This can be changed from the app/restify.php file:

'base' => '/api/restify',

Middleware

One important configuration is the restify default middleware:

// config/restify.php

'middleware' => [
    'api',
    // 'auth:sanctum',
    
Binaryk\LaravelRestify\Http\Middleware\DispatchRestifyStartingEvent::class,
    Binaryk\LaravelRestify\Http\Middleware\AuthorizeRestify::class,
]

Generate repository

Creating a new repository can be done via restify command:

php artisan restify:repository PostRepository

If you want to generate the Policy, Model, and migration as well, then you can use the –all option:

php artisan restify:repository PostRepository --all

Generate policy

Since the authorization is using the Laravel Policies, a good way of generating a complete policy for an entity is by using the restify command:

php artisan restify:policy PostPolicy

Above are the details of the basic setup & configurations if you want to implement its Authentication & API setup, you can visit its complete details on its documentation and source code on Github.

Published at : 18-02-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project