【问题标题】:Route does not exist when redirecting to resource route [Laravel]重定向到资源路由时路由不存在[Laravel]
【发布时间】:2015-11-09 02:59:26
【问题描述】:

我定义了以下路线:

Route::resource('profile', 'ProfileController', ['except' => ['create', 'destroy']]);

但是,当我尝试使用以下命令重定向到 profile/{id} 方法时:

redirect()->route('profile', [$userId]);

我收到以下错误:

InvalidArgumentException in UrlGenerator.php line 278:
Route [profile] not defined.

可能是什么问题?

【问题讨论】:

    标签: php laravel redirect routes resources


    【解决方案1】:

    尝试这种方式重定向

    return redirect('profile/2');
    

    【讨论】:

    • 试过了,但它似乎根本没有重定向。如果我在 ProfileController 的 index 方法中这样做有关系吗?我正在尝试进行设置,以便 /profile 显示登录用户的个人资料,而 /profile/{id} 显示其他人的。
    • 实际上,是的,这段代码有效。这是我的错,因为我没有返回重定向。
    • 如果此代码有效,请投票。这将对其他人有所帮助
    【解决方案2】:

    路由方法将路由名称作为第一个参数。所有特定资源路由都会有自己的名称,但没有使用基础资源(配置文件)的名称创建。

    通过运行php artisan route:list,您将看到所有路线及其名称的列表。对你来说,它应该是这样的:

    +--------+----------+-----------------------------------------+-----------------+---------------------------------------------------+------------+
    | Domain | Method   | URI                                     | Name            | Action                                            | Middleware |
    +--------+----------+-----------------------------------------+-----------------+---------------------------------------------------+------------+
    |        | POST     | profile                                 | profile.store   | App\Http\Controllers\ProfileController@store      |            |
    |        | GET|HEAD | profile                                 | profile.index   | App\Http\Controllers\ProfileController@index      |            |
    |        | GET|HEAD | profile/create                          | profile.create  | App\Http\Controllers\ProfileController@create     |            |
    |        | PATCH    | profile/{profile}                       |                 | App\Http\Controllers\ProfileController@update     |            |
    |        | PUT      | profile/{profile}                       | profile.update  | App\Http\Controllers\ProfileController@update     |            |
    |        | DELETE   | profile/{profile}                       | profile.destroy | App\Http\Controllers\ProfileController@destroy    |            |
    |        | GET|HEAD | profile/{profile}                       | profile.show    | App\Http\Controllers\ProfileController@show       |            |
    |        | GET|HEAD | profile/{profile}/edit                  | profile.edit    | App\Http\Controllers\ProfileController@edit       |            |
    +--------+----------+-----------------------------------------+-----------------+---------------------------------------------------+------------+
    

    因此,既然您似乎打算显示用户个人资料,那么您应该这样做:

    redirect()->route('profile.show', [$userId]);
    

    【讨论】:

    • 太完美了。我已将此更改为答案,因为使用->route() 方法似乎比redirect('/profile/' . $id 干净得多。
    猜你喜欢
    • 2017-12-30
    • 2016-06-07
    • 2016-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    • 1970-01-01
    相关资源
    最近更新 更多