SASM
SASM

Reputation: 1312

Password reset routing laravel

The user receives the password reset url in the email which is of format http://example.com/reset/passwordresetcode. I have a route defined for this link as

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

On clicking the link in the email, a view containing a form is rendered to reset the password. This form consists of email, password and password confirm fields and I plan to grab the passwordresetcode directly from the url. I have been able to get this passwordresetcode in my view.

Now to process the form post, I have the route defined as:

Route::post('reset', 'UserController@passwordReset');

How can I get the passwordresetcode in this controller action passwordReset? I know I can have a hidden field in my form to receive it on post, but it does not quite look the laravel way and surely there must be a better way. Kind of new to laravel :). Thanks

Upvotes: 0

Views: 739

Answers (2)

SASM
SASM

Reputation: 1312

Modifying the route defined for the post to

Route::post('reset/{code}', 'UserController@passwordReset');

did the trick. And in the controller, I can get the passwordresetcode by doing

public function passwordReset($code)
{ 
      echo $code;
}

However if the validation fails, trying to redirect like

return Redirect::route('reset/'.$code)->withInput()
                ->withErrors($validation)
                ->with('title', 'resetrequestfailure')
                ->with('message', 'Seems like you made some errors.');

seems not to work. I tried using the named routes as well, that too did not work.

Upvotes: 0

davidnknight
davidnknight

Reputation: 462

You could use a hidden input where you pass the code from your controller method to the view and this way the code will be posted with the rest of your form data to passwordReset on submit.

{{ Form::hidden('passwordresetcode', $passwordresetcode) }}

Or you could use a flash variable to temporarily store it in the session:

Session::flash('passwordresetcode', 'value');

And in your next controller method (passwordReset), simply retrieve it:

Session::get('passwordresetcode');

You can read more about flash variables in the official documentation.

Upvotes: 1

Related Questions