Home » Php » Controller redirect back to a POST form

Controller redirect back to a POST form

Posted by: admin December 5, 2017 Leave a comment

Questions:

I have a table showing a list of names, with an “edit” button and hidden id value at the side.
Clicking the “edit” button will post the hidden id as a form value and display the edit page so the user can change details of that person. Pretty standard.

When editing the details and submitting I’m using a validator. If the validation fails it needs to go back to the edit page and display the errors. Problem is that the edit page required an Id value via POST method, but the redirect only seems to utilise the GET method which leads to a “Controller Method Not Found” error as there is no Get route set up.

Does anyone know how I can redirect back to the page via POST not GET. Currently my code looks like:

public function postEditsave(){
    ...
    if ($validator->fails())
    {
        return Redirect::to('admin/baserate/edit')
        ->withErrors($validator)
        ->withInput();
    }else{ 
                ...

thanks

Answers:

You can use Redirect::back()->withInput();

You may wish to redirect the user to their previous location, for example, after a form submission. You can do so by using the back method

See: http://laravel.com/docs/5.0/responses

Questions:
Answers:

You don’t need to use POST to go to the edit page. You can use GET and a parameter to the route, check this out: http://laravel.com/docs/routing#route-parameters

You’d have a GET route to show the edit page, and a POST route to handle the request when the user submits the form.

It’ll look like this (notice the parameters):

public function getEdit($id)
{
    return View::make(....);

}

public function postEdit($id)
{
    ...
    return Redirect::back()->withErrors($validator)->withInput();
}

Questions:
Answers:

if “redirect with POST” is exist, then I don’t know it. I recomend you to just use flash data

Redirect::to('user/login')->with('id', 'something');

Questions:
Answers:

You can use Redirect::to("dashboard/user/$id")->withErrors($validator)->withInput();. You should use double quote to pass parameter if there is any errors with validation.

Leave a Reply

Your email address will not be published. Required fields are marked *