用于验证的自定义请求类给出405 Method Not Allowed Laravel 10

hc8w905p  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(92)

我尝试使用store方法存储数据,并使用VendorStoreRequest类进行验证。但是,我收到**:405 Method Not Allowed错误。当我更改为正常Illuminate Request**时,它可以工作。

  • 给出错误 *
use App\Http\Requests\VendorAccount\VendorStoreRequest;
...
public function store(VendorStoreRequest $request)
{
    return response()->json($request, 200);
}

字符串

  • 工作正常 *
use Illuminate\Http\Request;
...
public function store(Request $request)
{
    return response()->json($request, 200);
}


我正在使用资源API路由。

Route::prefix('vendor')->group(function () {
    Route::apiResource('/', VendorController::class)->parameters(['' => 'vendor']);
});


VendorStoreRequest.php

<?php

namespace App\Http\Requests\VendorAccount;

use Illuminate\Foundation\Http\FormRequest;

class VendorStoreRequest 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:5',
            'email' => 'required|unique:vendors|email',
            'phone' => 'required|digits:11|numeric|unique:vendors',
            'business_phone' => 'required|digits:11|numeric|unique:vendors',
            'password' => 'required|min:6',
            'business_name' => 'required|unique:vendors',
            'trade_licence_no' => 'required|regex:/^[0-9]+$/|max:20|unique:vendors',
            'bank_account_no' => 'required|regex:/^[0-9]+$/|max:20|unique:vendors',
            'nid' => 'required|digits_between:10,13|unique:vendors',
            'image' => 'required|image|mimes:jpg,png,jpeg'
        ];
    }

    public function messages()
    {
        return [
            'name.min' => 'Name should be at least 5 characters',
            'phone.digits' => 'Phone number is not valid',
            'phone.unique' => 'Phone number has already been taken',
            'business_phone.digits' => 'Business phone number is not valid',
            'business_phone.unique' => 'Business phone number has already been taken',
            'password.min' => 'Password should be at least 6 characters',
            'business_name.unique' => 'Business name has already been taken',
            'trade_licence_no.regex' => 'Trade licence number is not valid',
            'trade_licence_no.unique' => 'Trade licence number is duplicate',
            'bank_account_no.regex' => 'Bank account number is not valid',
            'bank_account_no.unique' => 'Bank account number is duplicate',
            'nid.digits_between' => 'NID number is not valid',
            'nid.unique' => 'NID number is duplicate',
            'nid.digits_between' => 'NID number is not valid',
        ];
    }
}

ccgok5k5

ccgok5k51#

在这里找到解决方案:
MethodNotAllowedHttpException using Form Request Validation on Laravel 5.5

accept:application/json和Content-Type:application/json

这样laravel就知道它是一个API请求,并将使用在API.php上定义的路由,而不是web.php路由。

相关问题