![]() |
Same tasks in each controller: controller inheritance? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Same tasks in each controller: controller inheritance? (/showthread.php?tid=12216) |
Same tasks in each controller: controller inheritance? - El Forum - 10-10-2008 [eluser]sl3dg3hamm3r[/eluser] Hey there I just wanted to ask for some advice if I am on the right track. Supposed I need to build an administration area, each controller which handles this area would need to check if the user is really logged in as administrator before letting him do anything. I thought I would write then first a general admin-controller who would check this in the constructor, and all other controllers would inherit from this one. This makes sure I won't forget this check anywhere. Similar to this example. Does this make sense to you experienced code-igniters, or is there a more elegant, better way? Glad for any hint :-) Oliver Same tasks in each controller: controller inheritance? - El Forum - 10-10-2008 [eluser]xwero[/eluser] a hook is the most elegant solution in my book. You create/fetch an authentication library and you add a post_controller_constructor hook that uses the authentication library. It can be a function if you want Code: function access() Same tasks in each controller: controller inheritance? - El Forum - 10-10-2008 [eluser]sl3dg3hamm3r[/eluser] I also thought about hooks. About loose coupling you are absolutely right, I might prefer this method, thx! p.s.: what book? Same tasks in each controller: controller inheritance? - El Forum - 10-10-2008 [eluser]xwero[/eluser] in one's book : According to one's opinion or way of thinking ![]() Same tasks in each controller: controller inheritance? - El Forum - 10-10-2008 [eluser]sl3dg3hamm3r[/eluser] ah... didn't know that one, took it for real :-)) Same tasks in each controller: controller inheritance? - El Forum - 10-10-2008 [eluser]Randy Casburn[/eluser] I do it with inheritance.... You can provide a folder through which all contents are secure. If the controller is in there...it is secure simply by being in there. There is little code required (a simple path change) to make this work. Remove it from the folder (change on path) and it is no longer secure. I agree with xwero that hooks are very elegant and I do like them. The one thing I don't like about hooks that that they are so dreadfully obscure. If you decide to use hooks, please, I emplore you comment, comment, comment your code in triplicate. Then put a text file in the root of your System directory named HOOKS that describes what hooks you have in place. The reason is that they work behind the scenes and will be doing things that you forget about. Just a thought. Randy Same tasks in each controller: controller inheritance? - El Forum - 10-10-2008 [eluser]xwero[/eluser] I agree with Randy that hooks can be a cause of bugs because they are not added in plain sight. But an easy way to know they are there is to add debug log messages to the hook function/methods you use. Same tasks in each controller: controller inheritance? - El Forum - 10-10-2008 [eluser]Randy Casburn[/eluser] Good point xwero...duh. I'll start doing that too. I have a full IDE with complete debugging capability. I'm stepping through the code with the debugger. So I don't rely on the CI logs too much. Even after all this time...perhaps I should. Thanks for the tip. Randy Same tasks in each controller: controller inheritance? - El Forum - 10-11-2008 [eluser]sl3dg3hamm3r[/eluser] [quote author="Randy Casburn" date="1223670292"]You can provide a folder through which all contents are secure. If the controller is in there...it is secure simply by being in there. There is little code required (a simple path change) to make this work. Remove it from the folder (change on path) and it is no longer secure.[/quote] I'm not sure if I understood that... can you provide a simple example? Same tasks in each controller: controller inheritance? - El Forum - 10-11-2008 [eluser]gon[/eluser] I also use inheritance from a base controller. The base controller has a _remap method, so every action will call _remap first. The _remap method checks the controller and the action, and checks if the user has permission to execute that controller / action. If access control is successful, the controller action is executed. Note that _remap only check ACL. Auth is checked in the base_controller constructor, and we get redirected out if not successful. This is the _remap method in base_controller: Code: ///////////////////////////////////////////// I also configure the menu, and call an optional XXX_init method which some controllers implement, but you can skip that. The ob_start(), ob_end() is another part you can skip if you want. I use it for getting the content that the action renders, and injecting it into the common layout. Regards. |