【发布时间】:2013-11-27 14:51:11
【问题描述】:
我正在使用 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):他试图在 GET 中将我重定向到 /password/remind,而这条路线(当然)在我的路线中不存在.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() }}
【问题讨论】:
-
谷歌搜索了一下,我发现了这个:culttt.com/2013/09/23/password-reminders-reset-laravel-4 在第一个 cmets 中,他们得到了同样的错误。有人说将路由从闭包移动到控制器解决了这个错误。也许你可以试试这个,看看会发生什么。
-
@ManuelPedrera 感谢您的建议。我试过了,没有成功……
标签: laravel-4 password-recovery