Laravel在电子邮件确认视图无效后立即更改密码

时间:2015-06-25 11:09:15

标签: php mysql laravel

创建用户后,将创建随机密码(和身份验证代码)并通过电子邮件发送给用户。

点击电子邮件中的链接后,用户状态将为1(非常活跃),用户可以立即更改密码。

现在它并没有像我想的那样工作。

UserController中:

public function store(CreateUserRequest $request, User $user, Attribute $attribute)
// some unnecessary code

if ((Input::get('usertype_id')) > 1) {
    $randomPassword = str_random(8);
    $user->password = Hash::make($randomPassword);

    $authentication_code = str_random(12);
    $user->authentication_code = $authentication_code;

    $user->active = 0;
};

$user->save();

if ((Input::get('usertype_id')) > 1) {
    // Email sturen met verficatie code
    $email = Input::get('email');

    Mail::send('emails.user', ['user' => $user, 'password' => $randomPassword, 'authentication_code' => $authentication_code], function ($message) use ($email) {
        $message->to($email, 'Lilopel')->subject('Lilopel: Verify your account!');
    });
};


public function confirmUser($authentication_code)
{
    if (!$authentication_code)
    {
        return 'auth code not found!';
    }

    $user = User::where('authentication_code', '=', $authentication_code)->first();

    if (!$user)
    {
        return 'user not found!';
    }

    $user->active = 1;
    $user->save();

    Session::put('user_id', $user->id);

    return view('user.setpassword', ['user' => $user]);
    //return redirect()->route('user.setPassword', [$user_id]);
}


public function setPassword(SetPasswordRequest $request)
{
    $user_id = Session::get('user_id');


    $user = $this->user->find($user_id);

    $user->fill($request->only('password'));

    $user->save();
}

路线:

Route::get('user/verify/{authenticationCode}', 'UserController@confirmUser');

Route::get('user/password', 'UserController@setPassword');

查看:

{!! Form::model($user, ["route"=>['user.setPassword', $user->id] , "method" => 'PATCH']) !!}

    <div class="form-group {{ $errors->has('password') ? 'has-error' : '' }}">
        {!! Form::label('password', trans('common.password'), ['class' => 'form-label col-sm-3 control-label
        text-capitalize']) !!}
        <div class="col-sm-6">
            {!! Form::password('password', ['name' => 'password', "class"=>"form-control","placeholder" =>
            trans('common.password') ]) !!}
            {!! $errors->first('password', '<span class="help-block">:message</span>') !!}
        </div>
    </div>
    <div class="form-group {{ $errors->has('confirm_password') ? 'has-error' : '' }}">
        {!! Form::label('password_confirmation', trans('common.confirmpassword'), ['class' => 'form-label
        col-sm-3 control-label text-capitalize']) !!}
        <div class="col-sm-6">
            {!! Form::password('password_confirmation', ['name' => 'password_confirmation',
            "class"=>"form-control","placeholder" => trans('common.confirmpassword') ]) !!}
            {!! $errors->first('password_confirmation', '<span class="help-block">:message</span>') !!}
        </div>
    </div>

{!! Form::submit( trans('common.edit'), ["class"=>"btn btn-primary text-capitalize center-block   "]) !!}

{!! Form::close() !!}

电子邮件链接有效,用户状态变为活动状态,但.blade会出现Route [user.setPassword] not defined. (View: public_html/server2/resources/views/user/setpassword.blade.php)错误。

1 个答案:

答案 0 :(得分:1)

一起工作要像你一样使用路线,你需要一个named route

更改此

Route::get('user/password', 'UserController@setPassword');

到这个

Route::get('user/password', [
    'as' => 'user.setPassword',
    'uses' => 'UserController@showProfile'
]);

另外,请确保路线​​的HTTP谓词和表单的method一起工作。