is it ok to have repeated codes in controllers |
[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.
[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-gu...asses.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 {
[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.
[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-gu...asses.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
[eluser]InsiteFX[/eluser]
If you do not want them to get access then add Code: // The underscore makes it un-reachable from the url
[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?
[eluser]InsiteFX[/eluser]
No, you would access it like any other method in the input class. Code: $this->input->newFunction();
[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???
[eluser]InsiteFX[/eluser]
Yes, but you will need to pass in a segment slug to do the view switching. |
Welcome Guest, Not a member yet? Register Sign In |