我正在使用Laravel 5.2并尝试使用其控制器创建密码更改表单。我添加了以下路线:
Route::get('changepassword', array('as' => 'reset.password', 'uses' => 'PasswordController@edit'));
Route::post('resetpasswordcomplete', array('as' => 'reset.password.complete', 'uses' => 'PasswordController@update'));
Http\Controllers\Auth\PasswordController
有以下方法:
public function edit() {
return View::make('auth/passwords/change');
}
public function update() {
$hasher = Sentinel::getHasher();
$oldPassword = Input::get('old_password');
$password = Input::get('password');
$passwordConf = Input::get('password_confirmation');
$user = Sentinel::getUser();
if (!$hasher->check($oldPassword, $user->password) || $password != $passwordConf) {
Session::flash('error', 'Check input is correct.');
return View::make('auth/passwords/change');
}
Sentinel::update($user, array('password' => $password));
return Redirect::to('/');
}
观点如下:
@if (Session::get('error'))
<div class="alert alert-error">
{{ Session::get('error') }}
</div>
@endif
{{ Form::open(array('route' => array('reset.password.complete'))) }}
{{ Form::password('old_password', array('placeholder'=>'current password', 'required'=>'required')) }}
{{ Form::password('password', array('placeholder'=>'new password', 'required'=>'required')) }}
{{ Form::password('password_confirmation', array('placeholder'=>'new password confirmation', 'required'=>'required')) }}
{{ Form::submit('Reset Password', array('class' => 'btn')) }}
{{ Form::close() }}
我收到ReflectionException
错误,因为我认为PasswordController
位于Auth文件夹中,因此只有想要使用auth脚手架重置其忘记密码的访客才能访问。我想知道如何允许登录用户访问此控制器,以便他们可以根据需要更改密码?
编辑:我在Alexy的解决方案后尝试执行以下操作: 公共职能__construct()
{
$this->middleware('guest', ['except' => ['resetpasswordcomplete', 'changepassword']]);
}
它仍然让我回到主页。
答案 0 :(得分:1)
将routes.php
中的控制器路径更改为:
Route::get('changepassword', array('as' => 'reset.password', 'uses' => 'Auth\PasswordController@edit'));
Route::post('resetpasswordcomplete', array('as' => 'reset.password.complete', 'uses' => 'Auth\PasswordController@update'));