Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
Problems with implementing "redirect" #41
I have been trying to get "redirect" working in order to preserve form data after submission, and have managed to break an app that was otherwise functioning pretty well.
I have a Controller method that processes data from a form, and I think what I am supposed to do is have this method use a "redirect" to point to another method in the Controller object by means of a route, registered in web.php, which points back to a different method in the Controller, said method being passed the Request object in order to re-display the page with all the session data as well as the form result. It all sounds rather convoluted to me, too.
The first method parses the data entered by the user into the form. In order to refresh the view of that form to include the result without losing the entered form data, I have a 'return' statement pointing to a redirect route, and including an array of the form data, including the result in 'returnMessage'.
I added a route to address this redirect, currently set to 'any' as a catch-all:
Back in the App Controller, I then added a 'showResults' function to accept the session data, which seemed to be how the Books example is set up,
Obviously this isn't working for me. I am now also getting a rather unhelpful error:
Serialization of 'Closure' is not allowed
...which appears to perhaps have something to do with how the routes are set up, but I do not know what.
At the moment I have three routes defined in web.php:
...and the path information from the Laravel error page seems to point to the second route as the source of the error. I'm sure it's something obvious to an experienced user.
Hi, sorry for the delay in responding.
I have been trying to follow the logic used in the foobooks example but I am obviously not implementing everything correctly.
The "Serialization of 'Closure' is not allowed" exception is thrown when I attempt to submit the form with input that passes validation.
I have been using a GET method with a form-action ( see convert.blade, line 22) corresponding to a route
It appears that the 'showResults' route is missing in the 'web.php' file:
Also, changing '$request' at line 119 in the 'ConvertController.php' file to '$unitType' seems to do the trick for the serialization problem.
Thanks for you reply; I see that I left that Route commented out for some reason; I was trying to change things to address one error and created another; also, thanks for pointing out the typo with $request.
I have a new error message now,
Yes, it is mostly working; I ran into problems with ensuring that the form data is always retained when the view is updated, whether by having an error triggered, or in the case where the user hits "Convert" and the data validates, resulting in a message. From your screenshot above, notice that the answer has appeared but the input fields, 'Unit Type', 'Conversion' and 'Enter Value' have reset to their default state; that was what I wanted to fix; perhaps at least for the first two inputs. It might not make sense to retain the third input field's value when refreshing the view after successful validation.