我有一个表单,应该去OrgController@store post到页面。发生的事情是,它似乎重定向到/,从来没有使它进入我的商店功能在OrgController。
根据我的网络选项卡在Firebug它post到/org/add然后使一个GET请求登录出于某种原因,即使我注释出我的调用认证中间件。
GET请求登录看到我登录,然后重定向到Laravel Auth RedirectIfAuthenticated设置的位置。(在这种情况下是/)。
基本上我希望我的路由尊重授权,但也POST到我的表单。
My Route是这样的
Route::group(['prefix' => 'org'], function () {
Route::get('search', 'OrgController@search');
Route::get('browse', 'OrgController@browse');
Route::get('add', 'OrgController@add');
Route::post('add', 'OrgController@store');
});
我的表单是这样的
<form method="post" action="">
<div class="form-group">
<label for="parent_org">Parent Organization</label>
<select class="form-control" id="parent_org" required="true">
<option>Org Unit 1</option>
<option>Org Unit 2</option>
<option>Org Unit 3</option>
<option>Org Unit 4</option>
</select>
</div>
<div class="form-group">
<label for="org_name">Org Unit Name</label>
<input type="text" class="form-control" id="org_name" required="true">
</div>
<div class="checkbox">
<label><input type="checkbox">Will users be added directly to this organization unit?</label>
</div>
<button type="submit" class="btn btn-primary">Add Org</button>
</form>
my php artisan route:list显示如下。
| | GET|HEAD | org | | App'Http'Controllers'OrgController@search | web,eventlog,auth |
| | POST | org/add | | App'Http'Controllers'OrgController@store | web,eventlog,auth |
| | GET|HEAD | org/add | | App'Http'Controllers'OrgController@add | web,eventlog,auth |
| | GET|HEAD | org/browse | | App'Http'Controllers'OrgController@browse | web,eventlog,auth |
| | GET|HEAD | org/search | | App'Http'Controllers'OrgController@search | web,eventlog,auth |
我使用的是自定义Auth提供程序,扩展了内置在Auth中的Laravel。
Thanks in advance
一个post路由不能正常工作,带您到登录页面。当你有一个带有method="post"动作是POST路由。如果表单没有安全令牌,如:
<form action="xxx" method="post">
...
<input type="hidden" name="_token" value="{{ csrf_token() }}">
...
</form>
将会发生的是,路由将总是重定向到Login,因为为了保证你的表单安全,因为它找不到Laravel的令牌,它将请求Login。
哎呀。
问题是我忘了我已经放入了异常处理程序。
if ($exception instanceof TokenMismatchException) {
return redirect('/login');
}
同样,我的表单中也没有CSRF令牌。因此,它抑制了无效的CSRF令牌错误,并重定向到登录。
. .教训。
为了安全起见,不要在登录页面或任何带有post请求的url中禁用令牌。只需在表单字段中添加{{csrf_token()}}
。Csrf_token返回一个带有令牌的输入隐藏字段。它可以保护表单免受外部的滥用攻击。
<form method="post" action="">
<div class="form-group">
<label for="parent_org">Parent Organization</label>
Add csrf_token anywhere inside your form
{{csrf_token()}}
<select class="form-control" id="parent_org" required="true">
<option>Org Unit 1</option>
<option>Org Unit 2</option>
<option>Org Unit 3</option>
<option>Org Unit 4</option>
</select>
</div>
<div class="form-group">
<label for="org_name">Org Unit Name</label>
<input type="text" class="form-control" id="org_name" required="true">
</div>
<div class="checkbox">
<label><input type="checkbox">Will users be added directly to this organization unit?</label>
</div>
<button type="submit" class="btn btn-primary">Add Org</button>
</form>