在 JsonResponse 消息中显示字符 "

Display character " inside JsonResponse message

我正在使用 PHP 和 Symfony 开发 API,我需要在其中抛出一些错误消息。

我正在使用这个方法,当发生错误情况时我会调用它:

public function error($codeError, $message, $statusCode): JsonResponse
{
    return new JsonResponse([
        'code' => $codeError,
        'description' => $message
    ], $statusCode);
}

问题是,在变量 $message 中,有些情况下我需要抛出一条消息,如 Variable "user" is invalid,所以我传递了字符串 'Variable "user" is invalid'。但是我得到的 JsonResponse 的男孩是:"description": "Variable \"user\" is invalid".

如果没有这两个 \,我怎样才能使它看起来好看?

调用此函数的示例如下:

return (new ErrTC)->error('AUTH_ERROR', 'Variable "user" is invalid', 401);

您得到的响应非常好。

如果双引号字符是字符串的一部分,则需要对其进行转义,事实就是如此。

您始终可以用单引号而不是双引号将 user 括起来,这样就不需要转义了。

只要$message是:"Variable 'user' is invalid".