【问题标题】:Route returning Error 404 but the route exists路由返回错误 404 但路由存在
【发布时间】:2021-02-21 17:19:39
【问题描述】:

我有一个刀片模板,我在其中调用 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');

从我的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,但没有解决问题。我在这里做错了什么?

【问题讨论】:

  • php artisan route:clear 再试一次,希望能解决您的问题
  • 已经试过了,还是不行,不好意思,应该在帖子里说的。
  • 您在路线列表中看到您的路线了吗? php artisan route:list
  • 是的,都在那里
  • 检查控制器显示功能,可能是 404 从那里引发

标签: laravel


【解决方案1】:

如果你试试这个呢?

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

【讨论】:

    【解决方案2】:

    试试看,列出所有路由器

    
    php artisan route:list 
    
    

    或者你可以这样

    
    Route::resource('profile', 'ProfileController', [
        'names' => [
            'index' => 'show.show'
            'store' => 'profile.new',
            
        ]
    ]);
    

    【讨论】:

    • 我添加了 route:list 来提问,抱歉之前没有添加
    • 声明资源路线为我的,然后再试一次! php工匠路线:列表
    猜你喜欢
    • 2019-04-20
    • 1970-01-01
    • 2021-12-16
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 1970-01-01
    • 2013-07-27
    相关资源
    最近更新 更多