Welcome Guest, Not a member yet? Register   Sign In
avoiding repetition of code

I've been working on a site for sometime now and am currently at a stage where I'm just going back through and trying to clean things up a bit.
All five controllers use a couple of the same functions and right now they just repeat.
The most important one is _run() which all the methods call to display a page. Each controller's version of _run() is slightly different however they all share a few declarations that are exactly for same. For instance they all have a line of code which declares a $this->template['accountInfo'] variable so the loader file knows whether to display a login box or account options box. And they all do things like set the $this->template['title'] variable. However they differ for variables like $this->template['currentTab'] which is different for each controller (determines which tab appears selected in the navigation). Basically what i want is a way to do this
function _run(){
   //Paste code from a file which contains the standard declarations that all controllers need
   //declare the variables that are different like normal

can this be done??

Create a library or a helper.

the variables i need to declare are properties of the class

from a library or helper i can't declare $this->template['x'] because i am not in the controller namespace anymore
unless there is a way around this i dont know

I don't really understand what your doing, but you can always pass data around. Even if your not in the namespace of your library class, it would be once initialized $library->template['x'] = '';

I'll try that
each of my controllers has a variable called $template which is passed to the load->view function
so I want to assign keys in that variable $template. When i access that variable from a function within a class I go $this->template['key'] = ... so i didn't think that i could access that variable from within a library.

[eluser]Rick Jolly[/eluser]
Seems like you want some default class variables available to all of your controllers. You can extend the CI Controller to add your own variables and methods. Then your 5 controllers would extend that. Try your luck searching the forums for examples.

Thats exactly what i wanted !
I've made a MY_Controller which has the functions which all of my controllers had in common. For the function _run() which was in all my controllers but not always exactly the same I just made a second function called _global_run() which all of my _run() functions call. IN the _global_run() function all the common commands are made.

Thanks for the help!

Theme © iAndrew 2016 - Forum software by © MyBB