Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with implementing "redirect" #41

Closed
bibliodrone opened this Issue Apr 9, 2019 · 11 comments

Comments

Projects
None yet
4 participants
@bibliodrone
Copy link

bibliodrone commented Apr 9, 2019

Hi,

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'.

return redirect('/showResults')->with([
                'unitType' => $request,
                'sys' => $system,
                'valueToConvert' =>  $valueToConvert,
                'returnMessage' => $returnMessage
            ]);

I added a route to address this redirect, currently set to 'any' as a catch-all:

Route::any('/showResults', 'ConvertController@showResults');

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,

public function showResults(Request $request)
    {
        return view('layouts.calculate')->with([
                'unitType' => $request->session()->get('unitType', ''),
                'system' => $request->session()->get('system', ''),
                'valueToConvert' => $request->session()->get('valueToConvert',''),
                'returnMessage' => $request->session()->get('returnMessage', '') 
            ]);
    }

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:

Route::get('/', 'ConvertController@showForm');
Route::get('/convert_units', 'ConvertController@convertUnits');
Route::any('/showResults', 'ConvertController@showResults');

...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.

#pathInfo: "/convert_units"
#requestUri: "/convert_units?unitType=temperature&system=tometric&valueToConvert=45"

Thanks,

Gerry

@kisa7081

This comment has been minimized.

Copy link

kisa7081 commented Apr 9, 2019

Are there any anonymous functions in your controller?

@susanBuck

This comment has been minimized.

Copy link
Owner

susanBuck commented Apr 9, 2019

Hi Gerry - Can you share the Github URL for this project with the class? It'll be easier to help you troubleshoot if we can see the full picture.

@bibliodrone

This comment has been minimized.

Copy link
Author

bibliodrone commented Apr 10, 2019

Hi, sorry for the delay in responding.
The GitHub URL is https://github.com/bibliodrone/p3

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 '/convert_units' which resolves to ConvertController@convertUnits (line 34 in ConvertController). That function is to accept the (Request $request) parameter to allow the form to be displayed without losing the user's input, if there is any. I am not sure what the error message refers to, but based on the Laravel error screen, exception happens at the stage of saving session data. The specific highlighted line is "serialize($this->attributes)" which seems telling. So I must be using that process incorrectly. I've copied the relevant code below:

C:\xampp\htdocs\p3\vendor\laravel\framework\src\Illuminate\Session\Store.php
     *
     * @param  string  $data
     * @return string
     */
    protected function prepareForUnserialize($data)
    {
        return $data;
    }
 
    /**
     * Save the session data to storage.
     *
     * @return bool
     */
    public function save()
    {
        $this->ageFlashData();
 
        $this->handler->write($this->getId(), $this->prepareForStorage(
           serialize($this->attributes)
        ));
 
        $this->started = false;
    } 

Thanks,

Gerry

@kisa7081

This comment has been minimized.

Copy link

kisa7081 commented Apr 10, 2019

It appears that the 'showResults' route is missing in the 'web.php' file:

Route::get('/showResults', 'ConvertController@showResults');

Also, changing '$request' at line 119 in the 'ConvertController.php' file to '$unitType' seems to do the trick for the serialization problem.

return redirect('/showResults')->with([ 'unitType' => $unitType, 'sys' => $system, 'valueToConvert' => $valueToConvert, 'returnMessage' => $returnMessage ]);

@bibliodrone

This comment has been minimized.

Copy link
Author

bibliodrone commented Apr 10, 2019

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,
array_merge(): Expected parameter 2 to be an array, string given (View: C:\xampp\htdocs\p3\resources\views\convert\convert.blade.php)
That might be related to a view and/or redirect I suppose, but I am starting to see a pattern with Laravel error messages, namely that they are not very helpful at least while learning to use it.

@bibliodrone

This comment has been minimized.

Copy link
Author

bibliodrone commented Apr 10, 2019

The above "array_merge()" error appears to have been associated with an 'include' I was trying to use.

@kisa7081

This comment has been minimized.

Copy link

kisa7081 commented Apr 10, 2019

That's odd, your p3 is working fine for me.

image

@bibliodrone

This comment has been minimized.

Copy link
Author

bibliodrone commented Apr 10, 2019

kisa7081,

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.

@kisa7081

This comment has been minimized.

Copy link

kisa7081 commented Apr 10, 2019

It looks like what you had in your return from "convertUnits" was good as it was before, but take a look at "$sys" at line 120.

@liataun

This comment has been minimized.

Copy link

liataun commented Apr 14, 2019

Your production site appears to be retaining form data on both validation success and failure. Did you resolve your issue or am I misinterpreting what you are working on? (For instance, retaining values on browser refresh F5.)
@bibliodrone

@susanBuck

This comment has been minimized.

Copy link
Owner

susanBuck commented Apr 22, 2019

Closing this thread due to inactivity. Feel free to continue the discussion or start a new thread if the issue still exists.

@susanBuck susanBuck closed this Apr 22, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.