在网址中带有变量的Laravel路线

时间:2014-06-04 14:19:58

标签: authentication laravel laravel-4 forgot-password cartalyst-sentry

我正在尝试实现密码重置逻辑。用户获取重置电子邮件中密码的链接。网址看起来像

http://example.com/reset/resetcode

我为它定义了路线:

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

表单在视图中呈现以提交email, new password等。对于表单的帖子,我将路由定义为:

Route::post('reset/{resetcode}', array( 'as' => 'reset', 'uses' => 'UserController@passwordReset'));

我从resetcode控制器内的post route抓取passwordReset

public function passwordReset($resetcode)
{ 
    $validation = Validator::make(Input::all(), UserModel::$rulesPasswordReset);
    if ($validation->passes())
    { 
    try
    {
    // Find the user using the user email address
    $user = Sentry::findUserByLogin(Input::get('email'));

    // Check if the reset password code is valid
    if ($user->checkResetPasswordCode($resetcode))
    {
        // Attempt to reset the user password
        if ($user->attemptResetPassword($resetcode, 'new_password'))
        {
        // Password reset passed
         }
         else
         {
        // Password reset failed
         }
    }
    else
    {
        // The provided password reset code is Invalid
    }
        }
        catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
        {
       echo 'User was not found.';
        }
    }
    else return Redirect::route('reset')->withInput()
    ->withErrors($validation)
    ->with('title', 'resetrequestfailure')
    ->with('message', 'Seems like you made some errors.');
   }

我遇到的问题是在验证失败后我Redirect::route我从为resetcode定义的路由中获取post。验证失败时,重定向路由混乱,第二次无法获得resetcode。假设的格式网址

 http://example.com/reset/8f1Z7wA4uVt7VemBpGSfaoI9mcjdEwtK8elCnQOb

成为

http://bcnet.org/reset/%7Bcode%7D

它与路由的/{resetcode}部分有关,这是可变的,所以即使在验证失败后我怎样才能得到正确的resetcode,这意味着url保持不变。或者如何在验证失败后将其修复为适当的Redirect::route

1 个答案:

答案 0 :(得分:1)

您需要在退货时加入$resetcode

else return Redirect::route('reset', $resetcode)->withInput()
    ->withErrors($validation)