Everything You Need to Know About Dachshunds ? AN IMPORTANT Guide

Dachshunds are perhaps one of the most popular varieties of dogs in the world. They have a distinctive look, a gentle personality, and a loyal nature that make them ideal pets for families. But…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




The easiest way to avoid DRY for Api Response in Laravel.

Working with API responses while building a new API or integrating a third-party API is one of the most tedious works while working with Laravel.
In this article, I will show the simplest way you can avoid the repetition of responses.

By Author

Responses are mostly returned to consumers of the API indicating the status or returning a message along with some data.

Has our API endpoints Grow, the responses and messages will change, and having to manually assign these responses to each method within the controller will be something that will have to be repeated in other controllers as well.

We can follow the DRY principle by creating a response folder in our app folder. For this example, I created mine in App/Http/Response folder.

By Author

There is no artisan for this, you will have to manually add the folder and necessary response classes.

As you can see I have two responses named ApiError and ApiSuccess.

API error in this case will return responses relating to items that are not existing in the database.

By Author

ApiSuccess will return responses and response data for records in the database.

With this, we can easily modify our responses in one area and the changes will be automatically updated in controllers using the method.

By Author

The image above is a basic sample of how beginners and even some experience developers render their API responses.

There are lots of alternatives to making the code readable and cleaner, one could be the usage of collections and filters but for this article, we are focusing on the basic and most used by developers while building API.

With the usage of Response classes, our code will look like the image below.

By Author

This code is easy to read, modify and debug. imagine we want to change the ApiSuccess response message, we can easily do that within our ApiSuccess Class and the changes will take effect in our PhoneApiController methods using the ApiSuccess method.

In the working environment and design principle used by your company, you may find yourself using a different style like services, etc, but as long as the end result is to keep the code maintainable and scalable you are good to go.

Thanks for reading, and wishing you a career filled with fewer bugs.

Add a comment

Related posts:

The Trouble with Instagram Therapists

The New York Times recently posted an article titled Instagram Therapists Are the New Instagram Poets where the writer described how a new generation of internet savvy therapists are blowing up the…

Understanding Phases of the Writing Life

This morning I picked up an essay collection that I’m about halfway finished reading. It’s my intention to read an essay collection every month this year as part of my studying for writing my own. I…