如何解决 RouteCollection.php 行 218 中的 MethodNotAllowedHttpException:?

How to solve MethodNotAllowedHttpException in RouteCollection.php line 218:?

我的看法是这样的:

@foreach($users as $user)
    <tr>
        <td>{!! $user->id !!}</td>
        <td>{!! $user->username !!}</td>
        <td>{!! $user->phone !!}</td>
        <td>{!! $user->address !!}</td>
        <td>
            {!! Form::open(['route' => ['users.destroy.year', $user->id, $year], 'method' => 'delete']) !!}
            <div class='btn-group'>
                <a href="{!! route('users.edit', [$user->id]) !!}" class='btn btn-default btn-xs'><i class="glyphicon glyphicon-edit"></i></a>
                {!! Form::button('<i class="glyphicon glyphicon-trash"></i>', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs', 'onclick' => "return confirm('Are you sure?')"]) !!}
            </div>
            {!! Form::close() !!}
        </td>
    </tr>
@endforeach

我的routes\web.php是这样的:

Route::get('users/destroy/{year}', 'UserController@destroy')->name('users.destroy.year');

Route::resource('users', 'UserController');

我的控制器是这样的:

public function destroy($id, $year)
{
    $user = $this->userRepository->findWithoutFail($id);

    if (empty($user)) {
        Flash::error('User not found');

        return redirect(route('users.index.year', ['year' => $year]));
    }

    $this->userRepository->delete($id);

    Flash::success('User deleted successfully.');

    return redirect(route('users.index.year', ['year' => $year]));
}

存在这样的错误:

MethodNotAllowedHttpException in RouteCollection.php line 218:

url 看起来像这样:http://localhost/mysystem/public/users/2?2016

当点击按钮删除时,我希望 url 看起来像这样:http://localhost/mysystem/public/users/index/2016

有没有人可以帮助我?

HTML 表单不支持 PUT、PATCH 或 DELETE 操作。因此,在定义从 HTML 表单调用的 PUT、PATCH 或 DELETE 路由时,您需要向表单添加一个隐藏的 _method 字段。与 _method 字段一起发送的值将用作 HTTP 请求方法:

<form action="/foo/bar" method="POST">
    <input type="hidden" name="_method" value="DELETE">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>

在您看来,您已将表单方法声明为 DELETE;但是在 routes\web.php 文件中,您已将路由声明为 GET

更改视图

{!! Form::open(
    ['route' => ['users.destroy.year', $user->id, $year],
    'method' => 'get']
  )!!}

你的 routes/web.php 应该是这样的,

Route::get('users/index/{year}', 'UserController@index')
     ->name('users.index.year');
Route::get('users/destroy/{id}/{year}', 'UserController@destroy')
     ->name('users.destroy.year');
Route::resource('users', 'UserController', ['except' => ['index', 'destroy']]);

保持控制器代码不变。

这应该可以解决问题。


只是一个建议

最好不要对 GET 请求保留 delete 操作。所以像这样改变 DELETE 路线

Route::delete('users/destroy/{id}/{year}', 'UserController@destroy')
     ->name('users.destroy.year');

更改视图

{!! Form::open(
    ['route' => ['users.destroy.year', $user->id, $year],
    'method' => 'delete']
  )!!}

id 没有在路由中声明,可能是这个原因,所以改变你的路由:

Route::get('users/destroy/{year}', 'UserController@destroy')->name('users.destroy.year');

至此

Route::get('users/destroy/{id}/{year}', 'UserController@destroy')->name('users.destroy.year');

希望这能解决这个问题。