首页 > 解决方案 > 路由返回错误 404 但路由存在

问题描述

我有一个刀片模板,我在其中调用了 show 方法的路由:

<div class="user"><a href="{{ route('profile.show',$user->id) }}"><div><img src="{{asset('images/user.svg')}}" alt=""></div></a></div>

该路由由资源控制器控制,路由设置如下:

Route::resource('profile', 'App\Http\Controllers\ProfileController')->middleware('auth');

正如您从 my 的输出中看到的那样,定义了该控制器的所有路由php artisan route:list

    |        | GET|HEAD  | profile                     | profile.index     | App\Http\Controllers\ProfileController@index                            | web        |
    |        |           |                             |                   |                                                                        | auth       |
    |        | POST      | profile                     | profile.store     | App\Http\Controllers\ProfileController@store                            | web        |
    |        |           |                             |                   |                                                                        | auth       |
    |        | GET|HEAD  | profile/create              | profile.create    | App\Http\Controllers\ProfileController@create                           | web        |
    |        |           |                             |                   |                                                                        | auth       |
    |        | GET|HEAD  | profile/{profile}           | profile.show      | App\Http\Controllers\ProfileController@show                             | web        |
    |        |           |                             |                   |                                                                        | auth       |
    |        | DELETE    | profile/{profile}           | profile.destroy   | App\Http\Controllers\ProfileController@destroy                          | web        |
    |        |           |                             |                   |                                                                        | auth       |
    |        | PUT|PATCH | profile/{profile}           | profile.update    | App\Http\Controllers\ProfileController@update                           | web        |
    |        |           |                             |                   |                                                                        | auth       |
    |        | GET|HEAD  | profile/{profile}/edit      | profile.edit      | App\Http\Controllers\ProfileController@edit                             | web        |
    |        |           |                             |                   |                                                                        | auth       |

到目前为止,控制器只有一个 dd 函数,因为我还没有开始编写它,而 show 方法是唯一一个已经编写了任何东西的方法:

public function show($id)
{ dd($id);}

但由于某种原因,每当我尝试访问该路由时,它都会返回 404 Not Found 错误。我尝试访问 index 方法并且它可以工作,但是 show 方法总是返回 404 Not Found 错误。我也试过php artisan route:clear,但没有解决问题。我在这里做错了什么?

标签: laravel

解决方案


如果你试试这个?

<div class="user"><a href="{{ route('profile.show', ['profile' => $user->id]) }}"><div><img src="{{asset('images/user.svg')}}" alt=""></div></a></div>

推荐阅读