How to keep my controller thin and DRY? |
[eluser]Unknown[/eluser]
Hi. I think it was published before , but i didn't figure it right yet. My controllers got many common functions calls from the modules. Different controllers retreive the same data again in order to display data in different parts of my web site. For example: a block with items in cart meta tags for the header a block with category list I thought that i can create a controller extention (MY_controller) with the common functions and then the other controllers will extend it , but the application didn't start. What I was doing wrong and what is the recommended way to create controllers that fetch only the data that is required and don't repeat themself. thanks
[eluser]Dam1an[/eluser]
You say it didn't work when you tried to create MY_Controller... can we see the code for MY_Controller, and a controller which extends it that doesn't work... we might be able to spot something Do you get any errors (assuming you left error reporting to E_ALL) or just no output?
[eluser]Mushex Antaranian[/eluser]
You can create libraries, helpers or models (in most case I'd prefer libraries) with common functions and load them in MY_Controller constructor. Then just call functions and methods from any controller. Anyway, want to look to your code with Dam1an.
[eluser]tkyy[/eluser]
if you are reading this type of data from mysql for instance, you want to create a model if your information isnt frequently updated, you want to store it in the userdata or in a cookie file if you are getting slightly different information each time, you want to create a library wrapper for calls to your models or simply create more functions for each variation inside of one model
[eluser]Khoa[/eluser]
It sounds I'm also wondering the same thing with tzvika, or maybe not. But this is what I want to share between controllers but couldn't seem to find a way to achieve this. For example, I have 2 controllers in my website: projects and entries. Respectively, I have 2 sets of pages to: list projects/entries, view a project/entry details, add a project/entry, etc. And the codes for these controllers are very similar, let's look at list as an example: Code: class Project extends Controller Code: class Project extends Controller I tried this with a plugin, but inside the plugin I could not call the method $this->load->view() which I need to put there. Thanks.
[eluser]depthcharge[/eluser]
Try this, which is explained here in the userguide libraries/My_Controller.php Code: class My_Project extends Controller and in your controller file Code: class Project extends My_Project { |
Welcome Guest, Not a member yet? Register Sign In |