Controller redirect back to a POST form

14,873

Solution 1

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();
}

Solution 2

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

Solution 3

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');
Share:
14,873

Related videos on Youtube

Delmontee
Author by

Delmontee

🅱🆁🅴🅰đŸ…ē🅸đŸ…ŊđŸ…ļ 🆃🅷🅴 🅸đŸ…Ŋ🆃🅴🆁đŸ…Ŋ🅴🆃 🅾đŸ…Ŋ🅴 đŸ…ĩ🆄đŸ…Ŋ🅲🆃🅸🅾đŸ…Ŋ 🅰🆃 🅰 🆃🅸đŸ…ŧ🅴. ŲŠ(Ėžâ—ĖŽĖŽĖƒĖžâ€ĸĖƒĖž)Ûļ

Updated on September 14, 2022

Comments

  • Delmontee
    Delmontee over 1 year

    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

  • Delmontee
    Delmontee over 10 years
    Thanks for that. Ideally I didn't want to disclose the id within the url, ie admin/users/10/edit, but if it's the best option then I would go for it. How would I pass the id variable over to a declared controller, for example rather than: Route::get('admin/baserate/edit/{user}', function($user) ...I would need something like: Route::get('admin/baserate/edit/{user}',['uses' =>'BaserateController@getEdit'.....?
  • edpaez
    edpaez over 10 years
    I don't see the id in the url as something bad. But if you really wish they aren't shown, you can replace it with a string that maps to the id. Also, the parameters in the route will be the parameters in the function handling the route in the controller, so you can put the functions above inside the controller and the parameters will be filled just fine.
  • Delmontee
    Delmontee over 10 years
    Thanks, I tried that but it still goes via GET not POST. I guess if there was a way to do it via POST it would probably break the restful convention of Laravel. Shame though, I would have thought an issue like this would have cropped up more often when editing details of a record. Thanks again
  • Delmontee
    Delmontee over 10 years
    Perfect thanks, that works. It's not really an issue that the admin can see the ID in the url, I'm just quite new to Laravel and MVC's in general so I'm used to hiding all important info from view. Difficult habit to break