本文介绍了如何使用自定义请求(make:request)? (laravel)方法App \ Http \ Requests \ Custom :: doesExistI不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义请求以进行自己的验证.当我遵循这些文章时.

I created a custom request to make my own validation. As i follow these article.

我创建了 ProfileRequest

php artisan make:request ProfileRequest

在我的 ProfileRequest

<?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'
        ]; 
    }
}


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


My problem is when I use the ProfileRequest inside the controller, like below:

namespace App\Http\Controllers;

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

它返回如下错误:

Class App\Http\Controllers\ProfileRequest does not exist

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

I need your help Sirs. Somebody know how to use the custom request?

推荐答案

ProfileRequest 中,将extends FormRequest更改为extends Request.然后在类上方添加use Illuminate\Http\Request;.代码应如下所示.

In ProfileRequest change extends FormRequest to extends Request. Then add use Illuminate\Http\Request; above the class. Code should look like below.

<?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'
        ]; 
    }
}

然后将App\Http\Requests\ProfileRequest;放入您的控制器中.

Then put App\Http\Requests\ProfileRequest; in your controller.

这篇关于如何使用自定义请求(make:request)? (laravel)方法App \ Http \ Requests \ Custom :: doesExistI不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 22:01