drllqg2903 2017-08-06 15:51
浏览 58
已采纳

Laravel 5.3删除方法给出了方法NotOllowed with spoofing

I am trying to use Laravel's (5.3) destroy method to delete an object when I push a button, however I'm getting a MethodNotAllowedHttpException.

My route is

Route::delete('/admin/add/{id}', 'ItemController@destroy');

The destroy method is

public function destroy($id)
{
    $items = Item::where('id', $id)->get();
    foreach ($items as $item)
        $item->delete();
    return redirect('/admin/add');
}

My form is

<form action="{{ url('admin/add/'.$specific->id)}}" method="POST">
  {{ method_field('DELETE') }}
  {{ csrf_field() }}
  <div class="modal-footer no-border">
    <button type="submit" class="btn btn-primary">Delete</button>
  </div>
</form>

I checked the existing routes (php artisan route:list), and the delete one is showing:

DELETE   | admin/add/{id}   |          | App\Http\Controllers\ItemController@destroy

The error that I am getting is

in RouteCollection.php line 218
at RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'POST')) in RouteCollection.php line 205
at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD', 'POST')) in RouteCollection.php line 158
at RouteCollection->match(object(Request)) in Router.php line 766
at Router->findRoute(object(Request)) in Router.php line 621
at Router->dispatchToRoute(object(Request)) in Router.php line 607
at Router->dispatch(object(Request)) in Kernel.php line 268
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
at Pipeline->then(object(Closure)) in Kernel.php line 150
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 117
at Kernel->handle(object(Request)) in index.php line 53
at require('/Users/username/Sites/blades/public/index.php') in server.php line 133

Per the Laravel routing instructions, I'm using a form and spoofing the DELETE action using a POST method, but I'm still getting an exception. Any help would be appreciated.

NOTE: I followed the full example at https://laravel.com/docs/5.2/quickstart-intermediate, and it works perfectly fine there. My attempts at recreating this in a different project is what's causing the issue.

  • 写回答

2条回答 默认 最新

  • dousi3362 2017-08-06 22:20
    关注

    I found the problem. The page that this form was on had a different POST form above it, and I had closed the form with a </div> rather than a </form> tag. Because of that, it was trying to submit both forms at the same time, and the POST calls were conflicting.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行