【问题标题】:Password reset routing laravel密码重置路由 laravel
【发布时间】:2014-07-24 21:40:02
【问题描述】:

用户在电子邮件中收到密码重置网址,格式为http://example.com/reset/passwordresetcode。我为此链接定义了一条路线

Route::get('reset/{code}', function(){
    return View::make('users.reset_password');
});

单击电子邮件中的链接时,将呈现包含form 的视图以重置密码。此表单由emailpasswordpassword confirm 字段组成,我打算直接从网址中获取passwordresetcode。在我看来,我已经能够得到这个passwordresetcode

现在处理表单post,我将路由定义为:

Route::post('reset', 'UserController@passwordReset');

如何在此控制器操作passwordReset 中获得passwordresetcode?我知道我可以在我的form 中有一个隐藏字段以在post 上接收它,但它看起来不像 laravel 方式,肯定有更好的方式。对 laravel 来说有点新:)。谢谢

【问题讨论】:

    标签: php laravel laravel-4


    【解决方案1】:

    您可以使用隐藏输入,将代码从控制器方法传递到视图,这样代码将与您的其余表单数据一起发布到提交时的密​​码重置。

    {{ Form::hidden('passwordresetcode', $passwordresetcode) }}
    

    或者您可以使用 flash 变量将其临时存储在会话中:

    Session::flash('passwordresetcode', 'value');
    

    然后在您的下一个控制器方法(passwordReset)中,只需检索它:

    Session::get('passwordresetcode');
    

    您可以在官方文档中read more about flash variables

    【讨论】:

    • 我在考虑隐藏输入的相同方式,但我想知道是否有办法通过路由来做到这一点。
    • 如果你想通过路由来做,你可以在收到帖子数据后在 passwordReset 方法中使用Redirect::route('route.name')->with('passwordresetcode', 'value')->with('postData', Input::all());,但我不建议在没有必要且没有重定向的情况下使用重定向在这种情况下真正受益。实际上,由于重定向,您会使用户遭受更长的加载时间。
    • 我说的是第一手案例中的路线 Route::post('reset', 'UserController@passwordReset');您建议的方式假设我已经拥有密码重置代码。在那种情况下使用路由当然是没用的。
    【解决方案2】:

    修改为post定义的路由为

    Route::post('reset/{code}', 'UserController@passwordReset');
    

    成功了。在控制器中,我可以通过这样做得到passwordresetcode

    public function passwordReset($code)
    { 
          echo $code;
    }
    

    但是如果验证失败,尝试重定向

    return Redirect::route('reset/'.$code)->withInput()
                    ->withErrors($validation)
                    ->with('title', 'resetrequestfailure')
                    ->with('message', 'Seems like you made some errors.');
    

    似乎不起作用。我也尝试使用named routes,但也没有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-24
      • 1970-01-01
      • 2015-11-16
      • 2022-12-18
      • 2017-03-14
      • 1970-01-01
      • 1970-01-01
      • 2021-02-21
      相关资源
      最近更新 更多