Laravel 4密码提醒:重定向问题

时间:2013-11-27 14:51:12

标签: laravel-4 password-recovery

我正在使用Laravel 4密码提醒功能,如下所述:http://four.laravel.com/docs/security#password-reminders-and-reset。为了生成令牌,在password_reminder表中发送电子邮件并创建de DB记录,我使用路由文件中的标准代码:

Route::post('password/remind', function() {
  $credentials = array('email' => Input::get('email'));
  return Password::remind($credentials);
});

如果出现任何错误(例如未知的电子邮件地址),此代码会将返回发送给我的输入表单。而不是那样,我得到一个MethodNotAllowedHttpException。原因是Laravel没有尝试将我发送回我的表单URL(/password/forgot):他试图将我重定向到/password/remind,在GET中,这条路线不存在(当然)在我的routes.php文件中。

我检查了Illuminate\Auth\Reminders\PasswordBroker类的代码,它负责这个重定向,并找到了这个方法:

protected function makeErrorRedirect($reason = '')
{
  if ($reason != '') $reason = 'reminders.'.$reason;
  return $this->redirect->refresh()->with('error', true)->with('reason', $reason);
}

我将$this->redirect->refresh()替换为$this->redirect->back(),现在一切正常。但由于我无法在任何地方找到任何关于这个错误的评论,我认为我做错了什么......但我找不到什么!

这是我的routes.php文件:

Route::get('password/forgot', array('as' => 'forgot', 'uses' => 'SessionsController@forgot'));
Route::post('password/remind', function() {
    $credentials = array('email' => Input::get('email'));
    return Password::remind($credentials);
});
Route::get('password/reset/{token}', function($token) {
    return View::make('sessions.reset')->with('token', $token);
});
Route::post('password/reset/{token}', array('as' => 'reset', 'uses' => 'SessionsController@reset'));

我的SessionsController相关代码:

class SessionsController extends BaseController {

    [...]

    public function forgot() {
        return View::make('sessions.forgot');
    }

    public function reset() {
        $credentials = array(
            'email' => Input::get('email'),
            'password' => Input::get('password'),
            'password_confirmation' => Input::get('password_confirmation')
        );

        Input::flash();

        return Password::reset($credentials, function($user, $password) {
            $user->password = Hash::make($password);            
            $user->save();
            return Redirect::to('home');
        });
    }

}

最后是我的观看代码:

{{ Form::open(array('url' => 'password/remind', 'class' => 'form', 'role' => 'form', 'method'=>'post')) }}
    <div class="form-group">
        {{ Form::label('email', 'E-mail') }}
        {{ Form::text('email', '', array('autocomplete'=>'off', 'class' => 'form-control')) }}
    </div>
    {{ Form::submit("Envoyer", array("class"=>"btn btn-primary")) }}
{{ Form::close() }}

1 个答案:

答案 0 :(得分:0)

如果可能,我强烈建议您升级到Laravel 4.1,因为它提供了更灵活(也更容易理解和使用)的密码提醒/重置解决方案。

使用Laravel 4.1查看此示例:

https://github.com/laracasts/Laravel-4.1-Password-Resets/blob/master/app/controllers/RemindersController.php