Welcome Guest, Not a member yet? Register   Sign In
code duplication advice
#2

[eluser]jedd[/eluser]
Hi Winter,

This is a pretty common thing - and there's a few ways of doing it. I'm an advocate of the [url="http://ellislab.com/codeigniter/user-guide/general/core_classes.html"]extending the core controller approach[/url].

So, for example, in my file application/libraries/MY_Controller.php, within the constructor of same, I have a bunch of stuff - but this snippet is instructive:

Code:
$tmp_data['menu_data'] = $this->_generate_menu_data();
$this->data['main_menu_view'] = $this->load->view ('common/create_table_main_menu', $tmp_data, TRUE);


Elsewhere in the MY_Controller I have my _generate_menu_data() function that pretty much just returns one very big array( ... )

Anyway, the outcome of this is that every controller I have (which all extend MY_Controller rather than just Controller) now has $this->data['main_menu_view'] available - and they can also happily ignore it! (Because $this->data is the construct that I send to my view.) My view file is pretty sparse - it contains mostly echos of $data[] items in fact, wrapped in div's.

So for example, here's a section from my application/views/default.php (which delivers 99% of my pages):

Code:
<div id="div_navigation_menu">
     &lt;?php echo $main_menu_view; ?&gt;
</div>


Is this the kind of thing you were wondering about?

Database calls are slightly more complex, I gather, from the MY_Controller location - but not insurmountable. There are a a few ways, as I say, of providing this kind of 'everywhere' functionality.


Messages In This Thread
code duplication advice - by El Forum - 03-24-2009, 08:39 PM
code duplication advice - by El Forum - 03-24-2009, 08:53 PM
code duplication advice - by El Forum - 03-24-2009, 09:24 PM
code duplication advice - by El Forum - 03-26-2009, 03:07 AM
code duplication advice - by El Forum - 03-26-2009, 05:11 AM
code duplication advice - by El Forum - 03-26-2009, 12:26 PM
code duplication advice - by El Forum - 03-26-2009, 06:37 PM
code duplication advice - by El Forum - 09-30-2009, 02:31 PM
code duplication advice - by El Forum - 09-30-2009, 02:44 PM
code duplication advice - by El Forum - 09-30-2009, 02:49 PM
code duplication advice - by El Forum - 09-30-2009, 02:59 PM
code duplication advice - by El Forum - 09-30-2009, 03:34 PM
code duplication advice - by El Forum - 01-10-2010, 06:25 PM



Theme © iAndrew 2016 - Forum software by © MyBB