dscojuxf69080 2017-09-19 11:49
浏览 37
已采纳

Laravel 5.4 MethodNotAllowedHttpException

Having trouble with a route with two variables. I've got a form to delete an item.

<form action="/admin/room/{{$room->id}}/image/{{$img->id}}/">
    {{method_field('DELETE')}}
    {{csrf_field()}}
    <input class="btn btn-danger" type="submit" value="Delete">
</form>

Upon clicking Delete, I get a MethodNotAllowedHttpException.

Here's my routes file.

//authenticated users only
Route::group(['middleware' => 'auth'], function ()
{
    Route::prefix('admin')->group(function () 
    {
        /*Some other routes*/

        //Matches "/admin/room/{room}/image[s]?"
        Route::get('room/{room}/image', 'RoomImagesController@index')
        ->name('admin.roomimages');
        Route::get('room/{room}/image/create','RoomImagesController@create');
        Route::post('room/{room}/image','RoomImagesController@store');
        Route::delete('room/{room}/image/{image}','RoomImagesContoller@destroy');

        //Matches the "/admin/room[s]?" URL
        Route::get('/rooms', 'AdminController@rooms');      
        Route::get('/room/create', 'RoomController@create');
        Route::get('/room/{room}', 'RoomController@edit');  
        Route::post('/room/search','RoomController@search');
        Route::post('/room/store', 'RoomController@store'); 
        Route::post('/room/{room}','RoomController@update');    
        Route::delete('/room/{room}','RoomController@destroy');

        /*some more routes.*/
    }
}

And my RoomImagesController class

class RoomImagesController extends Controller
{
    /*Other Methods*/
    public function destroy(Room $room, RoomImage $image)
    {
        dd($room, $image);
    }
}

The URL I'm sent to is:

http://domain.name/admin/room/1/image/1?_method=DELETE&_token=/*tokenValue*/

Not sure why I'm having issues here! If anyone could shed some light I'd appreciate it. Let me know if you need any more information.


Edit

I've added method="post" to the form, with the same error. Here's a raw paste from the webpage.

<form method="post" action="/admin/room/1/image/4/">
    <input name="_method" value="DELETE" type="hidden">
    <input name="_token" value="lFrYPuzz8pY4EEJZEKCtZgjGM4dkk6uTD3p0jhpK" type="hidden">
    <input class="btn btn-danger" value="Delete" type="submit">
</form>

Edit 2

Named routes seemed like a solution!

//web.php
Route::post('room/{room}/image/{image}','RoomImagesContoller@delete')
    ->name('admin.deleteRoomImages');

//x.blade.php
<form action="{{ route('admin.deleteRoomImages', [ 
                        'room'  => $room->id, 
                        'image' => $img->id ]) }}/"
      method="POST">
    {{ method_field('DELETE') }}
    {{ csrf_field() }}
    <input class="btn btn-danger" type="submit" value="Delete">
</form>

But I still get the MethodNotAllowedError

It may be worth noting when I ran php artisan route:list I've found that i get a ReflectionError 'RoomImagesController does not exist' I'll investigate this further!

  • 写回答

6条回答 默认 最新

  • douxian1895 2017-09-19 14:56
    关注

    this is happening because you are making a request to wrong url

    it must be action="/admin/room/{{$room->id}}/image/{{$img->id}}" not action="/admin/room/{{$room->id}}/image/{{$img->id}}/"

    why it is happening you made a request to action="/admin/room/1/image/4/" which is being showing moved to action="/admin/room/1/image/4" and you lose method field which is DELETE that's why you are getting error try to use below example and it will work fine.

    <form action="/admin/room/{{$room->id}}/image/{{$img->id}}" method="post">
        {{method_field('DELETE')}}
        {{csrf_field()}}
        <input class="btn btn-danger" type="submit" value="Delete">
    </form> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dqwn64004 2017-09-19 11:53
    关注

    Add method="post" as an attribute to your form. Like so:

    <form action="/admin/room/{{$room->id}}/image/{{$img->id}}" method="post">
    

    If the method attribute is not provided, the form is submitted via GET by default. DELETE method requires the data to be submitted via POST.

    评论
  • douyanzan9145 2017-09-19 12:02
    关注

    Set a method attribute on the form to POST So you have

    <form action="/admin/room/{{$room->id}}/image/{{$img->id}}/" method="POST">
        {{method_field('DELETE')}}
        {{csrf_field()}}
        <input class="btn btn-danger" type="submit" value="Delete">
    </form>
    
    评论
  • dongtu4559 2017-09-19 12:02
    关注

    You can do it by POST method by passing method="POST" and add delete method hidden field inside the form tag

    <form action="/admin/room/{{$room->id}}/image/{{$img->id}}/" method="post">
        {{method_field('DELETE')}}
        {{csrf_field()}}
        <input class="btn btn-danger" type="submit" value="Delete">
    </form>
    
    评论
  • duanou3868 2017-09-19 12:31
    关注

    You could use Named Routes

    Route::delete('room/{room}/image/{image}',
                  'RoomImagesContoller@destroy')->name('deleteRoomImages');
    

    and use the route() helper method in your form

    <form action="{{ route('deleteRoomImages', [ 
                    'room'  => $room->id, 
                    'image' => $img->id ]) }}/" method="POST">
        {{ method_field('DELETE') }}
        {{ csrf_field() }}
        <input class="btn btn-danger" type="submit" value="Delete">
    </form>
    
    评论
  • doujing1156 2017-09-19 14:03
    关注

    Run composer dump-autoload to clear the ReflectionException. If you copied the class file and modified it, verify that the name of the class is RoomImagesController and run composer dump-autoload. Good luck!

    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥20 powerbulider 导入excel文件,显示不完整
  • ¥20 #关于multisim绘图遇到的问题
  • ¥15 用keil调试程序保证结果进行led相关闪烁
  • ¥15 paddle训练自己的数据loss降不下去
  • ¥20 用matlab的pdetool解决以下三个问题
  • ¥15 单个福来轮的平衡与侧向滑动是如何做到的?
  • ¥15 嵌入式Linux固件,能直接告诉我crc32校验的区域在哪不,内核的校验我已经找到了,uboot没有
  • ¥20 h3c静态路要求有详细过程
  • ¥15 调制识别中输入为时频图,星座图,眼图等
  • ¥15 数据结构C++的循环、随机数问题