18
FebLaravel 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.
Laravel Restify has a few requirements you should be aware of before installing it:
8.0
8.0
You can install this package via composer, Just run this command in the terminal:
composer require binaryk/laravel-restify
After the installation, the package requires a setup process:
php artisan restify:setup
The command above:
config/restify.php
configuration fileproviders/RestifyServiceProvider
and will add it in the config/app.php
app/Restify
directoryapp/Restif/Repository.php
app/Restify/UserRepository
repository for users CRUDHaving 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.
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
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 project