Laravel: load a view after posting


In Codeigniter I used to call the view function after posting data. Like below;

Ex: I have a show_products() function which will display list of products. When a user add a new product I'm posting data into add_product() function. If the process is successful I will not redirect to the products page, instead load the display function inside the add_product() like this:

//Inside the add_product() function



I think, there is no point of reloading the page again. Since we are already in the post function we can straight away set the view after the database insert.

However in laravel I see people redirecting after posting data.


//Inside the postProduct() function

   return Redirect::to('products');


I tried;

//Inside the postProduct() function

   $this->getIndex();// this is my product display function


but it didn't work.

Do we have a benefit by loading the view in the post function without redirecting every time?

If so how can I achieve the same thing with the laravel?

Thanks a lot!

It's not about the Laravel, instead, it's about a good or right way of doing things. In other words, it's a good programming practice to redirect to another route/page after you successfully submit a form.

Even in CodeIgniter or plain Php I do like this approach and encourage other developers to do that. So, the question is why this redirect is better than directly calling another method from the same request to show another view/page ?

This is the life cycle of the process:

  1. You post a form to a route/action page.
  2. You validate the submitted data and upon successful validation you insert the submitted data in to your database, otherwise you redirect back to that form with errors and old user inputs.

So. assume that, you have submitted a form and done saving the data into database successfully. After you save it you done something like this:

return View::make('...')->with('success', 'Data saved!');

In this case, your user can see the success message on the screen but what if the user, presses the f5 key from the keyboard to refresh the page (probably, accidentally), the form will be submitted to the same action again and the whole process will be repeated again.

So, if you had a redirect after form submission then, refreshing the page won't make any request to that form again.

Google search result on form resubmit on refresh., check the links, may be first one/two, you'll get better idea about the problem and the benefits of redirection after form submission.