The example in this article describes how Laravel implements customized error output content. Share it with everyone for your reference, the details are as follows:
Here is an analysis of laravel's verification of submitted data, how to customize the content of the error output
Run the command in the root directory
php artisan make:request PostUpdateRequest
will be in the appHttpRequests directory Create a PostUpdateRequest file
For example, I set
public function rules() { return [ 'posts_title' => 'required',//必填 ]; }
Add the following messages in the PostUpdateRequest file to define the error message
public function messages(){ return [ 'posts_title.required' =>json_encode( ['status'=>false,'message'=>'标题必须填'],JSON_UNESCAPED_UNICODE), ]; }
Pay attention to the above JSON_UNESCAPED_UNICODE
to do an experiment
echo json_encode("PHP中文网");
output Content:
"\u811A\u672C\u4E4B\u5BB6"
You can’t see what this is at all
Add a parameter
echo json_encode("PHP中文网", JSON_UNESCAPED_UNICODE);
Output:
"PHP中文网"
It’s normal, and suddenly I feel happy
I hope this article The above will be helpful to everyone in PHP programming based on the Laravel framework.
For more articles on how to implement custom error output content in Laravel, please pay attention to the PHP Chinese website!