简体   繁体   English

使用Redirect :: route()将变量插入命名路由

[英]Insert variable into named route using Redirect::route()

I am trying to redirect a route to a named router that contains a variable. 我正在尝试将路由重定向到包含变量的命名路由器。 Somehow the URL gets resolved into http://localhost:8000/users/reset_password?token=f3c6e64d3d5147fde8843af831ca4998 instead of http://localhost:8000/users/reset_password/f3c6e64d3d5147fde8843af831ca4998 (mind the ?token= ) 以某种方式将URL解析为http://localhost:8000/users/reset_password?token=f3c6e64d3d5147fde8843af831ca4998而不是http://localhost:8000/users/reset_password/f3c6e64d3d5147fde8843af831ca4998 (请注意?token=

Because of this mismatch in the URL that gets created, the incorrect route is used. 由于所创建的URL中的此不匹配,因此使用了错误的路由。

The Redirect::route() code looks like this: Redirect::route()代码如下所示:

        return Redirect::route('users.reset', array('token'=>$input['token']))
            ->withInput()
            ->with('error', $error_msg);

My routes are defined as follows: 我的路线定义如下:

Route::get('users/confirm/{code}', array('as' => 'users.confirm', 'uses' => 'UsersController@getConfirm'));
Route::get('users/forgot_password', array('as' => 'users.forgot', 'uses' => 'UsersController@getForgot'));
Route::post('users/forgot_password', array('as' => 'users.forgot', 'uses' => 'UsersController@postForgot'));
Route::get('users/reset_password/{token}', array('as' => 'users.reset', 'uses' => 'UsersController@getReset'));
Route::post('users/reset_password', array('as' => 'users.reset', 'uses' => 'UsersController@postReset'));
Route::resource('users', 'UsersController');

The command php artisan routes gives the next output: php artisan routes命令给出下一个输出:

+--------+---------------------------------------+------------------+----------------------------------+----------------+---------------+
| Domain | URI                                   | Name             | Action                           | Before Filters | After Filters |
+--------+---------------------------------------+------------------+----------------------------------+----------------+---------------+
|        | GET|HEAD users/confirm/{code}         | users.confirm    | UsersController@getConfirm       |                |               |
|        | GET|HEAD users/forgot_password        | users.forgot     | UsersController@getForgot        |                |               |
|        | POST users/forgot_password            | users.forgot     | UsersController@postForgot       |                |               |
|        | GET|HEAD users/reset_password/{token} | users.reset      | UsersController@getReset         |                |               |
|        | POST users/reset_password             | users.reset      | UsersController@postReset        |                |               |
|        | GET|HEAD users                        | users.index      | UsersController@index            |                |               |
|        | GET|HEAD users/create                 | users.create     | UsersController@create           |                |               |
|        | POST users                            | users.store      | UsersController@store            |                |               |
|        | GET|HEAD users/{users}                | users.show       | UsersController@show             |                |               |
|        | GET|HEAD users/{users}/edit           | users.edit       | UsersController@edit             |                |               |
|        | PUT users/{users}                     | users.update     | UsersController@update           |                |               |
|        | PATCH users/{users}                   |                  | UsersController@update           |                |               |
|        | DELETE users/{users}                  | users.destroy    | UsersController@destroy          |                |               |
+--------+---------------------------------------+------------------+----------------------------------+----------------+---------------+

The problem is you have two routes defined with the same name. 问题是您有两个定义了相同名称的路由。 Either your POST and GET routes have the same users.reset name. 您的POSTGET路由具有相同的users.reset名称。 And the second one is overwriting the first. 第二个覆盖第一个。 Change your name for POST route and you will be fine. 更改您的POST路线名称,一切都会好起来的。

I think the problem is that you also define the name of the variable, you only need to define the variables itself in the correct order: 我认为问题在于您还定义了变量的名称,只需要按照正确的顺序定义变量本身即可:

Redirect::route('users.reset', array($input['token'])
->withInput()
->with('error', $error_msg);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM