I'm working on an API using PHP and Symfony where I need to throw some error messages.
I'm working with this method, which I call when error conditions happened:
public function error($codeError, $message, $statusCode): JsonResponse
{
return new JsonResponse([
'code' => $codeError,
'description' => $message
], $statusCode);
}
The thing is, in the variable $message
, there are some cases that I need to throw a message like Variable "user" is invalid
, so I pass the string 'Variable "user" is invalid'
. But the JsonResponse
's boy I'm getting is: "description": "Variable \"user\" is invalid"
.
How can I make it look well, without the two \
?
An example of calling this function would be:
return (new ErrTC)->error('AUTH_ERROR', 'Variable "user" is invalid', 401);
CodePudding user response:
The response you are getting is perfectly fine.
The double-quote character needs to be escaped if it's part of a string, that's just the way it is.
You can always surround user
with single quotes instead of double quotes, and then no escaping would be necessary.
Just have $message
be: "Variable 'user' is invalid"
.