In this last lesson, let's check four packages that could be useful when working with APIs.
The first package, Laravel API Response Helpers, helps you to standardize responses.
If you return a lot of return response()->json()
, this package might help you. We can install this package via composer.
composer require f9webltd/laravel-api-response-helpers
Then, add trait F9Web\ApiResponseHelpers
to any Controller you want to use helpers. Now, we can use helpers from the package. For example, return a successful response:
use F9Web\ApiResponseHelpers; class ProductController extends Controller{ ApiResponseHelpers; public function index() { $products = Product::with('category')->paginate(9); return responseWithSuccess($products); }}
You can check other helper methods in the official documentation.
You can watch the video version of the package review on YouTube: Laravel Package For Standard API Responses.
This package allows you to create REST API easily.
The package can be installed via composer.
composer require tailflow/laravel-orion
To use this package, your Controller must extend the Orion\Http\Controllers\Controller
instead of a default Laravel Controller.
use Orion\Http\Controllers\Controller; class CategoryController extends Controller{}
Then, define the Model in the protected $model
property.
use App\Models\Category;use Orion\Http\Controllers\Controller; class CategoryController extends Controller{ protected $model = Category::class;}
Finally, for the Route, instead of the default Laravel Route
Facade, you must use the Orion
Facade.
routes/api.php:
use Orion\Facades\Orion; Orion::resource('categories', \App\Http\Controllers\Api\CategoryController::class);
We can check available routes using the artisan command php artisan route:list
.
Customization can be made using traits. For example, we can remove authorization.
use Orion\Concerns\DisableAuthorization; class CategoryController extends Controller{ use DisableAuthorization; protected $model = Category::class;}
We can check the response in the client.
Orion uses API Resources, which is why it is wrapped within the data
key. And, by default, pagination is added.
For more, check the official documentation.
You can watch the video version of the package review on YouTube: Laravel Orion: Build API With A Few Lines of Code.
Using this package with the artisan command, you can generate everything needed, from Models to Tests. The package can be installed via composer.
composer require essa/api-tool-kit
Now, we can generate. For example, generate a Post
model with the name as a string and the body as a text column.
php artisan api:generate Post --all "name:string|body:text"
As you can see, using the --all
flag, we have generated all the classes and routes that have been added to the routes/api.php
file.
The Resource has added all the fields.
app/Http/Resources/Post/PostResource.php:
class PostResource extends JsonResource{ public function toArray($request): array { return [ 'id' => $this->id, 'name' => $this->name, 'body' => $this->body, 'created_at' => dateTimeFormat($this->created_at), 'updated_at' => dateTimeFormat($this->updated_at), ]; }}
This package allows you to generate the start of a project quickly.
For more, check the official documentation.
The last package is similar to the third. Using an artisan command, the package generates a boilerplate for the API. The package also asks if you want to create Policy and Migrations. The package can be installed via composer.
composer require binaryk/laravel-restify
Then, packages require a setup process.
php artisan restify:setup
And run the Migrations.
php artisan migrate
Now, we can create a repository via the artisan command.
php artisan restify:repository Post --all
Besides the Migrations and Model fillable, fields should be added to the Repository.
app/Restify/PostRepository.php:
class PostRepository extends Repository{ public static string $model = Post::class; public function fields(RestifyRequest $request): array { return [ id(), field('name') ->required(), field('body') ->required(), ]; }}
Now, we can check the /api/restify/posts
endpoint. After adding some data, we can see the result.
For more on how to customize, check the official documentation.