【问题标题】:Post route instead of get LARAVEL发布路线而不是获取 LARAVEL
【发布时间】:2020-01-15 18:14:43
【问题描述】:

我想删除 laravel 应用程序中的用户(以管理员身份)。因此我有一条路线: Route::post('/dashboard/{id}/delete', 'Auth\Registercontroller@delete');

当我单击删除按钮时,会导致此链接出现此错误: The GET method is not supported for this route. Supported methods: POST. 我做的路线是POST路线,所以我不知道问题出在哪里。

【问题讨论】:

  • 您的按钮代码是什么样的? (或者如果有的话,表格)。
  • <a href="dashboard/{{$user->id}}/delete"><img src="imgs/X_mark.svg" class="remove_account" alt="cross" width="15px"></a>

标签: laravel routes


【解决方案1】:

为了遵守约定,这就是我要解决的问题。假设您正在使用 Blade 模板:

<form action="/dashboard/{{ $your_id_variable }}" method="POST">
    @method('DELETE')

    @csrf

    <button type="submit">Delete</button>

</form>

那么您的路线类型将是 delete,因为您已经在表单中对其进行了欺骗:

Route::delete('/dashboard/{id}', 'Auth\Registercontroller@destroy');

请注意,我已按照 Laravel 约定将控制器方法更改为 destroy

【讨论】:

    【解决方案2】:

    发生错误是因为它使用路由作为“GET”并且您将其定义为“POST”

    将您的路线修改为如下所示:

    Route::post('/dashboard/{id}/delete', 'Auth\Registercontroller@delete')->name('user.delete');
    
    <a href="#" onclick="event.preventDefault();document.getElementById('user-delete').submit();"> Delete </a>
        <form id="user-delete" action="{{ route('user.delete',$id) }}" method="POST" style="display: none;">
            @csrf
    
        </form>
    

    【讨论】:

      猜你喜欢
      • 2013-01-26
      • 2019-02-11
      • 2018-02-13
      • 2016-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-09
      • 1970-01-01
      相关资源
      最近更新 更多