Welcome Guest, Not a member yet? Register   Sign In
How to keep my controller thin and DRY?
#1

[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
#2

[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?
#3

[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.
#4

[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
#5

[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
{
   function list()
   {
      // Get list of projects from a model

      // Populate them into a $data['projects'] variable

      // Load the view and pass this $data variable over
   }
}

class Entry extends Controller
{
   function list()
   {
      // Get list of entries from a model

      // Populate them into a $data['entries'] variable

      // Load the view and pass this $data variable over
   }
}
Is there anyway that I could put this duplicated block of code in another controller, library, helper or whatever so that i dont have to duplicate them? I would like to be able to do something like this:

Code:
class Project extends Controller
{
   function list()
   {
      $generic = new Generic();
      $generic->list('project');
   }
}

class Entry extends Controller
{
   function list()
   {
      $generic = new Generic();
      $generic->list('entry');
   }
}

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.
#6

[eluser]depthcharge[/eluser]
Try this, which is explained here in the userguide

libraries/My_Controller.php
Code:
class My_Project extends Controller
{

  function My_Project()
  {
      parent::Controller();
      // .. any other code.....
  }

   function list()
   {
      $generic = new Generic();
      $generic->list('project');
   }
}

class My_Entry extends Controller
{

  function My_Entry()
  {
      parent::Controller();
      // ... any other code
  }

   function list()
   {
      $generic = new Generic();
      $generic->list('entry');
   }
}

and in your controller file
Code:
class Project extends My_Project {

  function Project()
  {
      parent::My_Project();

  }


}




Theme © iAndrew 2016 - Forum software by © MyBB