When I working with REST API with Laravel, I usually return a response that contains a message. Some of those response has custom message, and I want to set the other responses that has no custom message with a message provided by SymfonyHttpFoundation based on the response status code.
For example, I send a response that has 401 status code, the message should be "Unauthorized", and here is my usual code to do this:
return response()->json([ 'message' => 'Unauthorized', 'data' => $data ])->setStatusCode(401);
I am just wondering if there’s a method that I can call to set the message by the HTTP status code. Maybe just like
$message = SymfonyComponentHttpFoundationResponse::getStatusText(401). By doing this, I don’t have to retype the message manually and can dynamically set the response message field.
Any help will be appreciated. Thanks a lot.
Source: Symfony Questions