Home >php教程 >PHP开发 >How to implement custom error output content in Laravel

How to implement custom error output content in Laravel

高洛峰
高洛峰Original
2016-12-20 15:58:501575browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn