laravel:request验证器(10.27.0)

一,相关文档:

https://learnku.com/docs/laravel/10.x/validation/14856

二,php代码

1,生成类

liuhongdi@lhdpc:/data/laravel/dignews$ php artisan make:request HomeRequest

   INFO  Request [app/Http/Requests/HomeRequest.php] created successfully.

2,编辑代码:

app/Http/Requests/HomeRequest.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
 
namespace App\Http\Requests;
 
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Contracts\Validation\Validator;
 
class HomeRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        //return false;  说明:此处需修改为true
        return true;
    }
 
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            //
            'name' => 'required | min:2 | max:20',
            'age' => 'required | integer | min:18 | max: 110',
        ];
    }
 
    /**
     * 获取已定义验证规则的错误消息。
     *
     * @return array
     */
     public function messages()
    {
        return [
            'name.required' => '名字必须输入',
            'name.min' => '名字最短不少于两个字符',
            'name.max' => '名字最短不多于二十个字符',
            'age.required' => '年龄必须输入',
            'age.integer' => '年龄需为整数',
            'age.min' => '年龄需要大于18岁',
            'age.max' => '年龄最大不能超过110岁',
        ];
    }
 
   //重写返回json
   public function failedValidation(Validator $validator)
    {
        throw (new HttpResponseException(response()->json([
            'code' => 500,
            'msg' => $validator->errors()->first(),
        ], 200)));
    }
}

3,controller中应用验证器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
 
use App\Http\Requests\HomeRequest;
 
class NewsController extends Controller
{
    //获取请求参数
    public function home(HomeRequest $request){
         //所有参数
         $params=$request->all();  #获取所有参数
         //get方法
         $name1 = $request->get('name');
         //input方法
         $name2 = $request->input('name');
         //动态方法
         $name3 = $request->name;
         //从请求对象得到参数
         $name4 = request()->get('name');
 
        return ['params'=>$params,
                'name1'=>$name1,
                'name2'=>$name2,
                'name3'=>$name3,
                'name4'=>$name4,
        ];
    }

三,测试效果

没有提供参数时:

增加了验证后:

说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/10/18/laravel-request-yan-zheng-qi-10-27/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com

四,查看laravel框架的版本:

liuhongdi@lhdpc:/data/laravel/dignews$ php artisan --version
Laravel Framework 10.27.0
posted @ 2023-10-20 07:59  刘宏缔的架构森林  阅读(44)  评论(0编辑  收藏  举报