在Laravel 5.5上使用表单请求验证的MethodNotAllowedHttpException

时间:2018-01-07 09:45:48

标签: laravel validation laravel-5

当我在Laravel 5.5上使用Form Request Validation时,这很奇怪,我的所有帖子请求都将是405 Method Not Allowed,但是当我使用标准验证时正常,我的代码是:

php artisan route:list value

+--------+----------+----------------------------------------+--------------------+-----------------------------------------------------------+------------+
| Domain | Method   | URI                                    | Name               | Action                                                    | Middleware |
+--------+----------+----------------------------------------+--------------------+-----------------------------------------------------------+------------+
|        | POST     | api/register                           |                    | App\Http\Controllers\AuthController@register              | api        |
+--------+----------+----------------------------------------+--------------------+-----------------------------------------------------------+------------+

使用insomnia请求: enter image description here

我失眠的base_api值为http://mylocal.app/api

错误讯息:

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException

错误显示消息: enter image description here

路由器( api.php ):

Route::post('register', 'AuthController@register');

控制器( AuthController.php ):

use App\Http\Requests\StoreRegistration;

public function register(StoreRegistration $request)
{

    $email = $request->email;
    $name = $request->name;
    $password = $request->password;

    $user = User::create([
        'name' => $name,
        'email' => $email,
        'password'  => Hash::make($password)
    ]);

    $verifyUser = VerifyUser::create([
        'user_uuid' => $user->uuid,
        'token' => str_random(100)
    ]);

    SendVerificationEmail::dispatch($user); //I use queue to send email

    return response()->json(['success' => true, 'message' => 'message on success']);
}

StoreRegistration.php

class StoreRegistration extends FormRequest {
public function authorize()
{
    return true;
}

public function rules()
{
    return [
        'name' => 'required|min:4|max:50|unique:users|alpha_dash',
        'email' => 'email|required|max:255|unique:users',
        'password' => 'required|confirmed|min:6'
    ];
}
}

这个问题让我发疯。

2 个答案:

答案 0 :(得分:3)

当您从失眠发送请求时,请务必添加标题;

接受:application/jsonContent-Type: application/json

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

答案 1 :(得分:0)

错误并非来自验证。

这是因为当您使用 FormRequest 类时,您使用的是默认的 failedValidation 方法,该方法适用于网页并重定向到上一页错误。在您的情况下,您正在使用api但它重定向到不存在的URL。当您使用标准验证时,您指定了自己的逻辑。

throw new ValidationException($validator, $this->response(
        $this->formatErrors($validator)
    ));

因此,您需要使用api进行自定义失败的验证方法。您可以在 StoreRegistration 类中执行某些操作,您可以将 failedValidation 方法覆盖到以下内容 -

public $validator = null;
protected function failedValidation(\Illuminate\Contracts\Validation\Validator $validator)
{
    $this->validator = $validator;
}

然后使用您的控制器方法,使用 $ validator 对象执行任何操作。可能类似于以下内容 -

if (isset($request->validator) && $request->validator->fails()) {
    return response()->json($request->validator->messages(), 400);
}

看看之前我回答的this链接。

相关问题