【发布时间】:2019-06-21 05:51:13
【问题描述】:
我正在做一个简单的输入字段并将数据从表单提交到控制器,但总是得到 MethodNotAllowedHttpException。
blade.php
<form class="form-horizontal" method="post" action="sale/api">
<div class="form-group">
<label for="name" class="col-lg-2 control-label">
Subdomain Name
</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="name" name="name">
</div>
</div>
<div class="form-group">
<label for="api_key" class="col-lg-2 control-label">
Api Key
</label>
<div class="col-lg-10">
<input type="api_key" class="form-control" id="api_key" name="api_key">
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
控制器
public function sync()
{
$input = Input::only('name','api_key');
$user = new Sale;
$user->name = $input['name'];
$user->api_key = $input['api_key'];
Debugbar::info($user->name);
}
路线
Route::post('sale/api','SaleController@sync');
【问题讨论】:
-
在您的 from 中添加“@csrf”令牌。
-
action="sale/api"是一个相对 URL,只是一个问题邀请。执行action={{ action("SaleController@sync") }}以根据您的路线获取正确的 URL。 -
我总是使用
route-helper 来生成始终正确的路径 -
@apokryfos 同样的错误,我不确定缺少哪个部分。