is it ok to have repeated codes in controllers - 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: is it ok to have repeated codes in controllers (/showthread.php?tid=61117) |
is it ok to have repeated codes in controllers - El Forum - 09-22-2014 [eluser]rajneeshgobin[/eluser] hello i am quite new in CI, have these tables in mysql animal hobby and jobs i have these models animal_model hobby_model jobs_model then i have the coresponding controllers animalctrl,hobbyctrl jobsctrl in all 3 controllers i have nearly same functions such as insert,update ect where i basically copied and pasted 3 times is it ok to do like this or i should have one controller for all 3 tables. then pass parameters to know which model to load ect. is it ok to have repeated codes in controllers - El Forum - 09-22-2014 [eluser]CroNiX[/eluser] You can create a base controller (MY_Controller), or base model (MY_Model) to do those, and then have each of your controllers extend those (or model). See this (extending core classes section): https://ellislab.com/codeigniter/user-guide/general/core_classes.html You'd have something like: Code: class MY_Controller extends CI_Controller { Then you controllers would be like: Code: class Animal extends MY_Controller { is it ok to have repeated codes in controllers - El Forum - 09-23-2014 [eluser]treenef[/eluser] Some people like to keep all their mysql queries in their models, this follows a more stricter MVC pattern. But codeigniter is flexible in that you don't need to do this. Personally, I find having mysql code in my controllers easier to manage, that way I don't have to keep jumping through my files. Again this is personal preference. It is up to you. is it ok to have repeated codes in controllers - El Forum - 09-24-2014 [eluser]Andrewkha[/eluser] [quote author="CroNiX" date="1411401055"]You can create a base controller (MY_Controller), or base model (MY_Model) to do those, and then have each of your controllers extend those (or model). See this (extending core classes section): https://ellislab.com/codeigniter/user-guide/general/core_classes.html }[/code][/quote] So in this case the user will be able to type into address bar smth like http:\\site_name\MY_Controller\function_name\parameters How to avoid this? I do not want the user gets access to these functions directly is it ok to have repeated codes in controllers - El Forum - 09-24-2014 [eluser]InsiteFX[/eluser] If you do not want them to get access then add Code: // The underscore makes it un-reachable from the url is it ok to have repeated codes in controllers - El Forum - 09-24-2014 [eluser]Andrewkha[/eluser] Thanks for the response. And one more question related to class extension. If I extend, for example, Input class with newFunction. How do I need to access this newFunction? $this->MY_input->newFunction? or the regular way? is it ok to have repeated codes in controllers - El Forum - 09-24-2014 [eluser]InsiteFX[/eluser] No, you would access it like any other method in the input class. Code: $this->input->newFunction(); is it ok to have repeated codes in controllers - El Forum - 10-01-2014 [eluser]Webmister[/eluser] [quote author="rajneeshgobin" date="1411369932"]hello i am quite new in CI, have these tables in mysql animal hobby and jobs i have these models animal_model hobby_model jobs_model then i have the coresponding controllers animalctrl,hobbyctrl jobsctrl in all 3 controllers i have nearly same functions such as insert,update ect where i basically copied and pasted 3 times is it ok to do like this or i should have one controller for all 3 tables. then pass parameters to know which model to load ect. [/quote] Can i use one controller for all the view forms??? is it ok to have repeated codes in controllers - El Forum - 10-01-2014 [eluser]InsiteFX[/eluser] Yes, but you will need to pass in a segment slug to do the view switching. |