我正在使用 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);
uj5u.com熱心網友回復:
您得到的回應非常好。
如果雙引號字符是字串的一部分,則需要對其進行轉義,這就是它的方式。
你總是可以user用單引號而不是雙引號括起來,這樣就不需要轉義了。
剛好$message是:"Variable 'user' is invalid"。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/343629.html
