Password reset routing laravel - php

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

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.

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.

Related

How do I send a form but stay on the same page with phalcon?

In the form I have to define a handler like admin/login. Then it changes the page when I send it. I also tried using public function indexAction() handler and only using the controller admin without a handler, but then nothing happens.
Any help would be greatly appreciated.
Edit:
I wanted to have a error message but it was not so big a site that i wanted to make a form class.
The way i eventually fixed it was the following:
First i use $this->view->pick('admin/index'); in the handler to select my old view.
Next i send a variable like a error message with $this->view->setVars(['username' => $user->name,]); in the handler.
Last i used it with volt in the previous view views/admin/index.phtml like this {{ username }}
If you are using a form object you can then use the following code in your volt template
{{ form('myform', 'method':'post', 'role': 'form') }}
This will post your data to the same controller/action that invoked the form. From then you will be able to handle the posted data using valid() in the form as well as request->isPost()

Return with input if registration fails in Laravel

I know you can use:
return Redirect::back()->withInput(Input::all());
In any form, but register has its own classes in Laravel 5 after using make:auth and I am not sure how to redirect with input if registration fails.
Any help?
If you're using Form Request Validation, this is exactly how Laravel will redirect you back with errors and the given input.
If registration fails in Laravel, you can redirect the page with input data and error name which is mention by you in the controller. So, below code will be more helpful.
return redirect()->back()
->withInput($request->input())
->withErrors($errors, $this->errorBag());

Form action redirect to another controller Symfony2.8

I have a weird problem. I want to setAction to form to redirect to another controller.
I have 2 controllers for user and address. On route /{id}/modify we are in users controller twig and there is this, generated form:
$add=new Address();
$formAddress=$this->createFormBuilder($add)
->setAction($this->redirectToRoute("/{id}/addAddress",array('id'=>$id)))
->add("city","text")
->add("street","text")
->add("housenumber","text")
->add("flatnumber","text")
->add("send","submit")
->getForm();
After submitting I want to be redirected to address controller where form will be handled, route of address controller is /{id}/addAddress.
Thanks in advance for answers! Cheers!
Your action is not correct. If you use the function redirectToRoute it expect a route name.
// redirect to a route with parameters
return $this->redirectToRoute('blog_show', array('slug' => 'my-page'));
The first parameter (string) is the name for the route you try to send your form to. Otherwise you have to use redirect and use generateUrl to get the url which does the same but redirectToUrl is newer a shorter.
https://symfony.com/doc/current/controller.html

Pass value from one view to other laravel routes configuration

I have just started using laravel and currently using the version 5.2.
I am using two forms to get the data. First form in first view and second form in second view. I need to figure out a way to tell the second form, the form id of the first one, which the second form will be linked to.
I know this can be done by passing the values using the URL. But I lack the knowledge of the correct syntax.
1- How to send data while redirecting?
2- How should the route look like?
3- How to access that value in the second view, in order to pass that value when the second form submits?
I have googled a lot about this but couldn't understand those advanced syntax.
Any help is appreciated.
Thanks in advance.
This is the code in controller:
public function postCreateProfile(Request $request){
//Adding attributes from $request to $profile
$profile->save();
Session::flash('values',$request->azauj_id);
return redirect('/add/requirement');
}
public function getCreateRequirement(Request $request){
$att = Session::get('value');
Session::flash('value',$att);
return view('req');
}
public function postCreateRequirement(Request $request){
dd(Session::get('value'));
}
The forms are plain html forms with post methods of submission
When I use dd(Session::get('value'));, i get null. It means that the value is not being passed. To the postCreateRequirement method which is called when the second form is submitted.
Below are the routes.
//For Add Profile Page
Route::get('/add', 'ProfileController#getCreateProfile');
//For Add Profile Form Submission
Route::post('/add', 'ProfileController#postCreateProfile');
//For Add Requirements Page
Route::get('/add/requirement', 'ProfileController#getCreateRequirement');
//For Add Requirements Form Submission
Route::post('/add/requirement', 'ProfileController#postCreateRequirement');
1- How to send data while redirecting?
You can simply pass data with your redirect using the ->with() method which creates an session that will only appear on the next page more here
Example:
Say you want to send a status down to your view you add the with to your redirect:
// Where you are redirecting to
redirect("/otherRoute")->with("status", "Profile updated!");
// session name // data
Now you simply check if the session exist and echo it out:
// If the session does not exist it will return false and not create it
#if (session("status"))
<div class="alert alert-success">
// echo out the session
{{ session("status") }}
</div>
#endif
2- How should the route look like?
Routes should be defined in the routes.php file located in the http directory assuming you are posting the data you should make the routes and connect them to your controller like so:
//For Add Profile Page
Route::get('/add', 'ProfileController#getCreateProfile');
//For Add Profile Form Submission
Route::post('/add', 'ProfileController#postCreateProfile');
//For Add Requirements Page
Route::get('/add/requirement', 'ProfileController#getCreateRequirement');
//For Add Requirements Form Submission
Route::post('/add/requirement', 'ProfileController#postCreateRequirement');
3- How to access that value in the second view, in order to pass that value when the second form submits?
You could simply use the ->with() method in your redirect
public function postCreateProfile(Request $request){
//Adding attributes from $request to $profile
$profile->save();
return redirect('/add/requirement')->with("value",$request->azauj_id);
}
Get the value
public function getCreateRequirement(Request $request){
$value = session("value");
// compact it and trow it in an input to pass it trough to the last controller method
return view('req');
}
public function postCreateRequirement(Request $request){
$request->get("value");
}
OR
Create a global session and flush it afterwards
public function postCreateProfile(Request $request){
//Adding attributes from $request to $profile
$profile->save();
session("value",$request->azauj_id);
return redirect('/add/requirement');
}
Get the value
public function getCreateRequirement(Request $request){
return view('req');
}
public function postCreateRequirement(Request $request){
$value = session("value");
$request->session()->forget("value");
}
Note: Laravel does not flush sessions when somone logs out these will remain if not flushed by hand or using the method $request->session()->flush();

Laravel3: How can I forward post methods to action of controller?

Route::get('admin/user/add', function()
{
if (!Input::has('submit'))
return View::make('theme-admin.user_add');
else
Redirect::to('admin#user_add_process');
});
Normal GET request to admin/user/add shows the register form. However, when the form is submitted, I have to redirect it to action_user_add_process() function of Admin controller so I can save values to database.
The solution above doesn't work and I get 404's.
I call form action like this:
action="{{ URL::to('admin/user/add') }}">
How can I solve this issue?
Ps. If there is a shorter way to achieve this, let me know!
You need to specify that you are redirecting to a controller action using the to_action() method of Redirect.
return Redirect::to_action('admin#user_add_process');
Alternatively, you could just use this URL that doesnt even use the route you created making the if/else irrelevant.
{{ URL::to_action('admin#user_add_process') }}
On a third note, keeping your routes clean makes maintainence alot easier moving forward. As routes use a restful approach, take advantage of it. Using the same URL you can create and return the View with a GET request and submit forms with a POST request.
Route::get('admin/user/add', function() { ... }
Route::post('admin/user/add', function() { ... }
You can also have your routes automatically use a controller action like this:
Route::post('admin/user/add', 'admin#user_add_process');

Categories