使用 csrf_token Laravel 5.4 发送 post 表单时出现 MethodNotAllowedException
MethodNotAllowedException when send post form with csrf_token Laravel 5.4
我正在尝试从我的路由资源访问我的 "store" (POST) 方法。我从可以具有不同输入的表单调用 ProductCRUD.store 方法,具体取决于上一页。我的意思是,根据我接收到的对象是否具有特定的布尔值,显示输入。
@if($category->havePrice == 1)
{!! Form::label('price', 'Price') !!}
{!! Form::text('price', '', array('id' => 'price', 'name' => "price") !!}
@endif
当我点击按钮发送表单时,我收到一条消息说
RouteCollection.php 第 251 行中的 MethodNotAllowedHttpException:...
我也尝试使用 csrf_field() 方法添加 csrf 令牌。
我试图用这两种方法清空 laravel 的缓存:
php artisan cache:clear
php artisan route:cache
我尝试创建这样的个人路线:
Route::post('/product/store', array('as' => 'product', 'uses' => 'ProductController@store'));
None 这些解决方案有效。总是那个例外。
我当前的代码:
路线:
Route::resource('ProductCRUD','ProductController');
表格
<form method="POST" action="{{route('ProductCRUD.store')}}">
{{ csrf_field() }}
控制器方法:
public function store(Request $request) {
$newGameModel= new GameModel();
$category = $this->CategoryRepo->getByID($request->category);
$this->validate($request, [
'name'=> 'required',
'picture'=> 'required|image|mimes:jpg,jpeg,gif,png|max:5000',
'quantite' => 'required|min:0',
'price' => 'required_if:'.$category->havePrice.',1',
]);
...
}
如果我删除“$this->validate(...)”方法,代码将继续执行(不再有 MethodNotAllowedException 错误)。我试图一一评论验证它没有改变。
另外,如果我重新加载页面,输入错误会正确显示。如果我再次点击发送表格,惊喜!又报错了。
编辑:
我只使用一个按钮和表单发送我的表单,没有 ajax:
{!! Form::submit('Next step', 'name'=>"accept", 'methode' => "post")) !!}
另外,我可以访问我的控制器代码。如果我删除验证方法,一切正常 find
你们知道它为什么这样做吗?感谢您的宝贵时间!
好的,所以我找到了问题以及如何解决它(不是我想做的)。
首先,我的代码没有任何错误。我的post, csrf 和我的路由都很好
问题是幼虫如何验证事物。例如,如果您的第一页是 post 表单而您的第二页也是 post 表单,当您在第二页上验证失败时,它将尝试 return 到第二页显示错误。但事实是,它找不到你第一页的响应,所以你得到了像我一样的错误。
我通过删除第一个表格来修复它(对我来说,这是我可以放在其他地方的一些信息)。但是不知道有没有更好的办法解决。
我正在尝试从我的路由资源访问我的 "store" (POST) 方法。我从可以具有不同输入的表单调用 ProductCRUD.store 方法,具体取决于上一页。我的意思是,根据我接收到的对象是否具有特定的布尔值,显示输入。
@if($category->havePrice == 1)
{!! Form::label('price', 'Price') !!}
{!! Form::text('price', '', array('id' => 'price', 'name' => "price") !!}
@endif
当我点击按钮发送表单时,我收到一条消息说 RouteCollection.php 第 251 行中的 MethodNotAllowedHttpException:...
我也尝试使用 csrf_field() 方法添加 csrf 令牌。 我试图用这两种方法清空 laravel 的缓存:
php artisan cache:clear
php artisan route:cache
我尝试创建这样的个人路线:
Route::post('/product/store', array('as' => 'product', 'uses' => 'ProductController@store'));
None 这些解决方案有效。总是那个例外。
我当前的代码:
路线:
Route::resource('ProductCRUD','ProductController');
表格
<form method="POST" action="{{route('ProductCRUD.store')}}">
{{ csrf_field() }}
控制器方法:
public function store(Request $request) {
$newGameModel= new GameModel();
$category = $this->CategoryRepo->getByID($request->category);
$this->validate($request, [
'name'=> 'required',
'picture'=> 'required|image|mimes:jpg,jpeg,gif,png|max:5000',
'quantite' => 'required|min:0',
'price' => 'required_if:'.$category->havePrice.',1',
]);
...
}
如果我删除“$this->validate(...)”方法,代码将继续执行(不再有 MethodNotAllowedException 错误)。我试图一一评论验证它没有改变。
另外,如果我重新加载页面,输入错误会正确显示。如果我再次点击发送表格,惊喜!又报错了。
编辑: 我只使用一个按钮和表单发送我的表单,没有 ajax:
{!! Form::submit('Next step', 'name'=>"accept", 'methode' => "post")) !!}
另外,我可以访问我的控制器代码。如果我删除验证方法,一切正常 find
你们知道它为什么这样做吗?感谢您的宝贵时间!
好的,所以我找到了问题以及如何解决它(不是我想做的)。 首先,我的代码没有任何错误。我的post, csrf 和我的路由都很好
问题是幼虫如何验证事物。例如,如果您的第一页是 post 表单而您的第二页也是 post 表单,当您在第二页上验证失败时,它将尝试 return 到第二页显示错误。但事实是,它找不到你第一页的响应,所以你得到了像我一样的错误。
我通过删除第一个表格来修复它(对我来说,这是我可以放在其他地方的一些信息)。但是不知道有没有更好的办法解决。