在Laravel 5.5上使用表单请求验证的MethodNotAllowedHttpException [英] MethodNotAllowedHttpException using Form Request Validation on Laravel 5.5
问题描述
当我在
我在失眠中的base_api值为
路由器( api.php ):
Route :: post('register','AuthController @ register');
控制器( AuthController.php ):
使用App \ Http \ Requests \ StoreRegistration;公共功能寄存器(StoreRegistration $ request){$ email = $ request-> email;$ name = $ request->名称;$ password = $ request->密码;$ user = User :: create(['名称'=>$ name,'email'=>$ email,'password'=>Hash :: make($ password)]);$ verifyUser = VerifyUser :: create(['user_uuid'=>$ user-> uuid,'token'=>str_random(100)]);SendVerificationEmail :: dispatch($ user);//我使用队列发送电子邮件返回response()-> json(['success'=> true,'message'=>'成功消息']));}
StoreRegistration.php :
Class StoreRegistration扩展了FormRequest {公共功能authorize(){返回true;}公共职能规则(){返回 ['名称'=>'required | min:4 | max:50 | unique:users | alpha_dash','email'=>'email | required | max:255 | unique:users','password'=>必需|确认|分钟:6"];}}
这个问题使我发疯.
当您因失眠而发送请求时,请确保添加标头.
接受: application/json
和 Content-Type:application/json
laravel通过这种方式知道它是一个api请求,将使用在 api.php
上定义的路由,而不是在 web.php
路由上定义的路由.
This is pretty strange when i use Form Request Validation on Laravel 5.5, all my post request gonna be 405 Method Not Allowed, but getting normal when i use standard validation, here my code is:
php artisan route:list value
+--------+----------+----------------------------------------+--------------------+-----------------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+----------------------------------------+--------------------+-----------------------------------------------------------+------------+
| | POST | api/register | | App\Http\Controllers\AuthController@register | api |
+--------+----------+----------------------------------------+--------------------+-----------------------------------------------------------+------------+
Request using insomnia:
My base_api value in insomnia is http://mylocal.app/api
Error message:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
Error display messages:
Router (api.php):
Route::post('register', 'AuthController@register');
Controller (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'
];
}
}
This problem makes me crazy.
when you send the request from insomnia make sure to add the headers;
accept: application/json
and Content-Type: application/json
that way laravel knows that its an api request and will use routes defined on api.php
and not web.php
routes.
这篇关于在Laravel 5.5上使用表单请求验证的MethodNotAllowedHttpException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!