如何使用自定义请求(make:request)?(laravel) 方法 App\Http\Requests\Custom::doesExistI 不存在

gec*_*cko 4 laravel laravel-request laravel-5.7

我创建了一个自定义请求来进行自己的验证。当我关注这些文章时。

我创建了ProfileRequest

php artisan make:request ProfileRequest
Run Code Online (Sandbox Code Playgroud)

在我的个人资料请求

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ProfileRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required|min:10',
            'age' => 'required|numeric'
        ]; 
    }
}
Run Code Online (Sandbox Code Playgroud)


我的问题是当我在控制器内 使用 ProfileRequest 时,如下所示:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
class ProfileController extends Controller
{
    public function update(ProfileRequest $request){
        return "123";
    }
}
Run Code Online (Sandbox Code Playgroud)

它返回如下错误:

Class App\Http\Controllers\ProfileRequest does not exist
Run Code Online (Sandbox Code Playgroud)

先生们,我需要你们的帮助。有人知道如何使用自定义请求吗?

小智 8

ProfileRequest中更改extends FormRequestextends Request. 然后添加到use Illuminate\Http\Request;类上面。代码应如下所示。

<?php

namespace App\Http\Requests;

use Illuminate\Http\Request;

class ProfileRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required|min:10',
            'age' => 'required|numeric'
        ]; 
    }
}
Run Code Online (Sandbox Code Playgroud)

然后放入App\Http\Requests\ProfileRequest;您的控制器。