Home > php教程 > PHP开发 > body text

How to implement custom error output content in Laravel

高洛峰
Release: 2016-12-20 15:58:50
Original
1515 people have browsed it

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
Copy after login

will be in the appHttpRequests directory Create a PostUpdateRequest file

For example, I set

public function rules()
{
  return [
    'posts_title' => 'required',//必填
  ];
}
Copy after login

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),
    ];
}
Copy after login

Pay attention to the above JSON_UNESCAPED_UNICODE

to do an experiment

echo json_encode("PHP中文网");
Copy after login

output Content:

"\u811A\u672C\u4E4B\u5BB6"
Copy after login

You can’t see what this is at all

Add a parameter

echo json_encode("PHP中文网", JSON_UNESCAPED_UNICODE);
Copy after login

Output:

"PHP中文网"
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template