📌  相关文章
📜  方法 Illuminate\Routing\Route::get 不存在 (1)

📅  最后修改于: 2023-12-03 15:10:24.957000             🧑  作者: Mango

方法 Illuminate\Routing\Route::get 不存在

当在 Laravel 应用程序中使用 Illuminate\Routing\Route::get 方法时,可能会遇到以下错误提示:

BadMethodCallException: Method Illuminate\Routing\Route::get does not exist.

这是因为在 Laravel 5.2 之后,get 方法已经被废弃,取而代之的是 match 方法。

解决方法

这个错误可以通过将 get 方法替换为 match 方法来解决。在 web.php 或者 api.php 路由文件中,将原本的代码:

Route::get('/example', 'ExampleController@index');

替换为:

Route::match(['get', 'head'], '/example', 'ExampleController@index');

如果你只需要支持 GET 请求,可以将 match 方法替换为 get 方法:

Route::get('/example', 'ExampleController@index');

同时,如果你仍想支持 PUT、PATCH、DELETE 和 OPTIONS 请求,可以使用以下代码片段:

Route::match(['get', 'head', 'post', 'put', 'patch', 'delete', 'options'], '/example', 'ExampleController@index');
结论

在 Laravel 5.2 以后的版本中,get 方法已经被废弃,应该使用 match 方法。通过将代码从 get 方法替换为 match 方法,即可消除此错误。