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

时间:2015-03-26 11:06:02

标签: laravel laravel-4 laravel-routing

我正在尝试将路由重定向到包含变量的命名路由器。不知何故,网址被解析为http://localhost:8000/users/reset_password?token=f3c6e64d3d5147fde8843af831ca4998而不是http://localhost:8000/users/reset_password/f3c6e64d3d5147fde8843af831ca4998(请注意?token=

由于创建的URL不匹配,因此使用了错误的路由。

Redirect::route()代码如下所示:

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

我的路线定义如下:

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');

命令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          |                |               |
+--------+---------------------------------------+------------------+----------------------------------+----------------+---------------+

2 个答案:

答案 0 :(得分:1)

问题是您有两个使用相同名称定义的路由。您的POSTGET路由具有相同的users.reset名称。第二个是覆盖第一个。更改POST路线的名称,你会没事的。

答案 1 :(得分:-1)

我认为问题在于您还定义了变量的名称,您只需要以正确的顺序定义变量本身:

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