【问题标题】:Laravel routes error get requestLaravel 路由错误获取请求
【发布时间】:2016-09-06 19:23:20
【问题描述】:

设置一个简单的get请求似乎会抛出一个http错误,这是我目前的代码:

链接:

<a href="{{ route('account.single', ['id' => $account->id]) }}">

路线:

Route::get('/admin/overview/{id}', [
    'uses' => '\App\Http\Controllers\AdminController@getSingle',
    'as' => 'account.single',
    'middleware' => ['auth', 'admin'],
]); 

在网址中显示为 - admin/overview?id=5

控制器:

public function getSingle($id)
{
    return view('admin.account');
}

这是我得到的错误 - notFoundHttpException in RouteCollection.php line 161:

请注意,如果我从路由 url 中删除 /overview/ 使其显示为 /admin/{id} 它可以工作,但 get 请求找不到它从 url 中找到 /overview/ 的 id

【问题讨论】:

  • 您的 URL 应该是 /admin/overview/5 而不是 /admin/overview?id=5,这可能就是您收到 404 Not Found 的原因。

标签: php laravel laravel-5


【解决方案1】:

正如 Jonathon 的评论中提到的那样,您的路线的网址确实应该按照您的意图显示为 /admin/overview/5,而不是 /admin/overview?id=5。


要确定问题,您需要:

  • 仔细检查您没有看到缓存的路由 - 在命令行中运行:php artisan route:clear
  • 检查您的路线以确保您没有在您的routes.php 文件上到达另一条路线,例如admin/{some_variable}。你可以使用php artisan route:list 来查看 Laravel 正在寻找的路线列表
  • 如果您发现另一个导致问题的路线,请调整您的 routes.php 文件中的顺序,将具有更高特异性的路线放置在具有较低特异性的路线之上(示例如下)。

Route::get('/admin/overview/{id}', [
    'uses'       => '\App\Http\Controllers\AdminController@getSingle',
    'as'         => 'account.single',
    'middleware' => ['auth', 'admin'],
]); 

Route::get('/admin/{some_variable}', [
    'uses'       => '\App\Http\Controllers\AdminController@getSomething',
    'as'         => 'account.something',
    'middleware' => ['auth', 'admin'],
]);

【讨论】:

    【解决方案2】:

    您的网址不正确。你应该像这样构建 url:

    /admin/overview/2

    使用 laravel 辅助函数:

    route('account.single', ['id' => 2])

    【讨论】:

    • 这正是 OP 正在做的事情。
    猜你喜欢
    • 2021-08-31
    • 2017-04-21
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 2023-03-23
    • 2019-10-29
    相关资源
    最近更新 更多