Working with Users - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Working with Users (/showthread.php?tid=43213) |
Working with Users - El Forum - 07-04-2011 [eluser]caen[/eluser] Hello, I'm getting started with CodeIgniter (trying to learn the MVC approach to program design, as well as trying to learn how to use a framework), and I want to create a simple application. The basic flow would be 1. Check if the user is authenticated. 2. If the user is not authenticated, display and process a login form. 3. If the user is authenticated, display a page and process some input. I've managed to create the authentication code (step 2) and the input processing (step 3), but now I don't know how to go about step 1. I have two ideas, and I want to know if they are good, and how you would improve them. The first idea is to create an Index controller which checks to see whether the user is authenticated, and, if he or she is, redirect the user to the controller for step 3. Otherwise, redirect the user to the authentication page. It seems simple enough. The only downside I can see is that I am writing a third controller and that when someone accesses the website, the first thing they get is a redirect (this is particularly painful for a mobile user). The second idea is to load the controller I wrote for user authentication and have something like if the user is authenticated, redirect to the controller for step 3; otherwise, go to step 2 inside the constructor. This also seems simple, but I don't know if it is the right approach. Any thoughts on this matter? I also don't want suggestions for authentication libraries, as I'm just trying to play with the framework, not create anything complex. Thanks in advance! Working with Users - El Forum - 07-04-2011 [eluser]toopay[/eluser] Having bunch of lines in EVERY controller constructor, not just about having ugly and un-maintainable code, but it's indeed break the OO rules itself. Since this is an OO issues, there are one thing you shouldnt missed : Object Inheritance. In this case, you may consider to this : 1. Declare a "base" controller for each type of your controller. 2. Create some automatic procedure that separate your controller type. 3. Specify some global variable or function, so you (D)ont ®epeat (Y)ourself. You can do that by creating MY_Controller.php, and put that into your 'core' folder inside application folder. I just will show you the idea... Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); And since you already declare a global variable, like user authentification checks (it only run once) you doesn't need to repeat an authentification procedure and execute a library function everywhere in your controller to check user state, now you can use $this->is_login to determine that. |