CodeIgniter Forums

Full Version: Load->view or redirect?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Most of my code is load->view because I've been following the local news tutorial. But there's a piece of code I snatched online and it uses redirect('login/index').

I could probably use load->view instead of redirect, but what are advantages and disadvantages using one or another? Or when to load views or when redirect?
A view is simple to load your template. If you make an redirect you will send the user after an action to another site (e.g. after an formular submit or an method action).
A redirect will end the current request and tell the user's browser to move to a different URL, either on an external site or within your own site. The primary reason you would use a redirect within your own site would be to prevent reproducing the same code in multiple locations on your site (or when you've moved functionality to a different URL and haven't set the routing to route the old location to the new one).

For example, redirect('login/index') would presumably send the user to a login form which is served by a controller which handles the authentication functionality for your site (we could presume it is called login, but it may be routed to something else). Then you don't have to reproduce that code elsewhere on your site, you simply redirect people to that URL segment any time they need to log in.

Another reason this method might be preferred over loading a view for the login form would be that the same controller method could handle GET and POST requests for the form, so the code to load the view and set any required variables for the GET request doesn't have to be duplicated if the form has to be re-displayed (e.g. to display error messages). Of course, if the POST passed validation, the method would generally redirect the user to some other page, rather than re-displaying the login form to someone who has already logged in.
@Rufnex and mwhitney: Thank you!

I don't understand 'you don't have to reproduce that code elsewhere on your site'. What is meant by reproducing the code?
Reproduce: duplicate, copy, repeat.

Maybe it starts out with me just using the following to login:

Maybe later I add some data that needs to be passed to the view, so I have something like:
$data = array('something' => $something, 'nothing' => $nothing);
$this->load->view('login_form', $data);

...and before I know it, my code to load my login form is more like this:
$something = $this->db->where($somethingWhere)->get('something');
$nothing = $this->db->where($nothingWhere)->get('nothing');
$data = array('something' => $something, 'nothing' => $nothing);
$this->load->view('login_form', $data);

So, every time I need a user to login to my site, I copy & paste this block of code into my controller. Each time I have to change it I run around and paste the new code over the old code. My code to manage logging in is touching all of my other code, and every time I have to change something in it, I have to touch every piece of code that might ever result in someone logging in.

Instead of doing all of that, I can just use redirect('login/index') and keep all of my code related to logging in (and logging out, and probably any other functionality related to authentication) in one place.
Kk, I understand what you meant by reproduce. Thank you!