Better Http Status Codes In Laravel
'Magic numbers' like
401 can cause a lot of confusion for colleagues or your future self. It's not always immediately obvious what these numbers represent.
A magic number is a number in the code that has no context or meaning.
Luckily, when it comes to HTTP Status Codes, we can make use of a complete set of constants that will make the meaning of your code self evident.
Response::HTTP_OK will return
Response::HTTP_UNAUTHORIZED will return
401 and my personal favourite
Response::HTTP_I_AM_A_TEAPOT will return
This is possible because the
Illuminate\Http\Response class extends the
Finally, we also have access to an array of all status codes via
Response::$statusTexts. This is handy if you want to list, validate or otherwise iterate over all status codes.