DDSameera's avatar

How to keep RESTFUL API Status Code in globally ? (Best Practice)

I want to keep all HTTP Status code in one global file with static method. What is the best practice to do this ? in laravel

Current Code

 return SendResponseTrait::sendError('No data found', "Warning", 404);          

Expected Code (status code declare as a class variable)

  return SendResponseTrait::sendError('No data found', "Warning", self::HTTP_UNPROCESSABLE_ENTITY);          

0 likes
2 replies
gMagicScott's avatar
Level 27

Laravel's Response class extends Symfony which already includes all the constants for status codes.

use Illuminate\Http\Response;

return Response::HTTP_UNPROCESSABLE_ENTITY 

By the way, 404 would be HTTP_NOT_FOUND. HTTP_UNPROCESSABLE_ENTITY maps to 422.

1 like

Please or to participate in this conversation.