Welcome Guest, Not a member yet? Register   Sign In
Best way to handle redirects within the same controller?
#1

[eluser]gunnarflax[/eluser]
Hi, I've always wondered what is the best practice for redirects within the same controller. If I have a controller like this:

Code:
class Test extends CI_Controller
{
   function index()
   {
       // ...
   }

   function number_two()
   {
       // Do something then show the index again
   }
}

In function number_two I do something and then redirects the user to index(). I can do this with both redirect() and $this->index(). I like to think that $this->index() is more efficient because I don't have to reload the whole page. But If I do it with method number 2 I get some problems with my logging function because it logs the current URL. So I've tried using a boolean as an argument ($this->index(TRUE)) which lets it know whether it should manually add the correct URL or try to automatically get it. I think it's a working solution except for one thing, if someone copies the URL after the redirect he doesn't come to the page he intended to since it still points to the controller method used before the redirect.

edit: And if I use redirect() I cannot utilize the function for showing form validation errors: validation_errors().

Please share your thoughts! Smile
#2

[eluser]theprodigy[/eluser]
I've always done basic redirect calls.

Using your example:
Code:
class Test extends CI_Controller
{
   function index()
   {
       // ...
   }

   function number_two()
   {
       // Do something then show the index again
       redirect('test/index');
   }
}

As for your 'edit' about form_validation errors, just make sure you only redirect if there are none.
#3

[eluser]toopay[/eluser]
Unless you want bypasses something in your controller, use redirect(). Just for your information, the http response code for each action you did above is different!




Theme © iAndrew 2016 - Forum software by © MyBB