[eluser]victorche[/eluser]
It's been a while, since I asked almost the same question and I still can not find the solution. For example, I have several controllers like "blog", "search", "faq".
But I have header everywhere (in every single page). And in this header, I have a navigation menu, with links, generated from a db result.
The header view looks like this:
Code:
<div id="menu">
<ul>
{top_menu}
<li><a href="/{link}">{link}</a></li>
{/top_menu}
</ul>
</div>
For every controller I have a model like "blog_model", "search_model", "faq_model" (mostly for any kind of db operations). But because the header is some kind of general (common) stuff, I created a general_model.php for use with these common site parts. Not sure if this is the right approach anyway ... And there I have a small function get_links() which takes all the menu links from a db table.
So in my MY_Controller.php I am loading this general model like this:
Code:
$this->load->model('general_model', 'general');
In every controller I have pieces of code like this:
Code:
$links = $this->general->get_links();
$data = array(
'top_menu' => $links,
// other data here ...
);
So my question is:
Can I avoid this duplication of code? Imagine the blog controller... I have there function index (for the main blog view), function categories (to display posts from custom category), function view (to view a single post with the comments). And everytime I have to use $links = $this->general ... and then in the $data array ... 'top_menu' => ...
I tried at least to deal with the $links variable. I put this line in MY_Controller.php like:
Code:
class MY_Controller extends Controller {
function MY_Controller()
{
parent::Controller();
$this->load->model('general_model', 'general'); // I think there is no other way of loading models globally? Can this be removed too?
$links = $this->general->get_links(); // The result is udefined variable: links