- Report this article
Atiqur Rahman
Atiqur Rahman
Software Engineer @Nhimex Software |Laravel|VueJs|Flutter|Python|ML|CV
Published May 9, 2023
+ Follow
The API response structure should be consistent and easy to understand. It should include a status code, a message, and the actual data being returned. The message should provide a brief explanation of the status code, and the data should be in a well-defined format, such as JSON or XML.
class BaseController extends Controller{ public function sendResponse($result, $message, $code=200): JsonResponse { $response = [ 'success' => true, 'data' => $result, 'message' => $message, ]; return response()->json($response, 200); } public function sendError($error, $errorMessages = [], $code = 404): JsonResponse { $response = [ 'success' => false, 'message' => $error, ]; if(!empty($errorMessages)){ $response['data'] = $errorMessages; } return response()->json($response, $code); }}
Please take a look at the example above. I have created two functions: one for sending a success response and another for sending an error response. This will assist frontend developers (or anyone using the functions) in understanding whether the response is an error or success. Based on that, they can show the appropriate response to the user. Otherwise, they would need to check each message or code to display the actual message to the user.
Then, you can inherit the class from the BaseController. Just an example:
Recommended by LinkedIn
class UserController extends BaseController{ public function index() { $users = User::all(); return $this->sendResponse($users, 'Users retrieved successfully.'); } public function show($id) { $user = User::find($id); if (is_null($user)) { return $this->sendError('User not found.'); } return $this->sendResponse($user, 'User retrieved successfully.'); }}
The UserController class inherits from the BaseController. It has two functions for showing all users and showing a specific user. These functions use the sendResponse and sendError functions from the BaseController to send the appropriate response to the frontend.
Which method do you follow? You can comment your opinion.
Like
Celebrate
Support
Love
Insightful
Funny
5
2 Comments
Pacifique Linjanja
Software Engineer - Javascript | Typescript | NestJs | NodeJs | GraphQL | web3 | solidity | Rust
3mo
- Report this comment
The way I always design my APIs is pretty similar to this, however, I also leave a place for an optional error object, especially when the API may be used by external teams
1Reaction
Martin Njuguna
Software Developer
8mo
- Report this comment
I prefer using custom Traits. The classes work more or less like helper functions and are available globally; which makes them flexible and re-usable. See the screenshot attached as a simple example.
No more previous content
No more next content
1Reaction
See more comments
To view or add a comment, sign in
More articles by this author
No more previous content
- Do you know how computers execute high-level Programming language? Nov 3, 2022
No more next content
Sign in
Stay updated on your professional world
Sign in
By clicking Continue to join or sign in, you agree to LinkedIn’s User Agreement, Privacy Policy, and Cookie Policy.
New to LinkedIn? Join now
Insights from the community
- Web Development How do SOAP and GraphQL differ?
- Web Services API What are some common GraphQL web services API errors and how do you debug them?
- Data Analytics What are the main differences between JSON and XML data formats?
- Web Servers How do you keep up with the latest trends and standards of JSON and XML data in web communication?
- Programming How does XML work in SOAP?
- Web Applications How do GraphQL and REST differ in their error handling mechanisms?
- Web Applications How do you use web application APIs to visualize data?
- Software Development How do you choose between JSON and XML for data exchange?
- Entity Framework How do you test your DbContext and related services in a DI container?
- Programming How do you handle pagination in GraphQL?
Others also viewed
- Measure Node.js performance with perf_hooks Module! Mateo Scarafia 1y
- A Comparison Between REST API and GraphQL API with Example Code in React EDEH ISAAC 1y
- Node.js cluster forking and using the 5th channel! Joe Acosta 5y
- Designing Your HTTP JSON APIs Output Ulisses Herrera Freire de Almeida 5y
- What is an API ? Rajesh T 2mo
- Searching Data In JSON Md Muhimenul Tareque 2y
- Stop using nested ifs. Do this instead Hasibul Islam 1y
- A complete guide on how to implement Json Web Tokens (JWT) in your project using Node.js Leo Carten 7mo
- How to Use process.env to Access Environment Variables in Node.js (wdio framework). Adnan Muzaffar 1y
Explore topics
- Sales
- Marketing
- IT Services
- Business Administration
- HR Management
- Engineering
- Soft Skills
- See All