Welcome Guest, Not a member yet? Register   Sign In
Autoloading a set function in all the controllers
#10

[eluser]TheFuzzy0ne[/eluser]
You'd simply run the functions you need from within the controller's constructor.
Code:
class Blog extends Controller {

    function Blog
    {
        parent::Controller(); # Don't forget to call the parent constructor!
        $this->load->library('navigation');
        $this->set_navigation();
    }
    
    // Should be executed in a hook maybe?
    function set_navigation()
    {
        $this->navigation->add_item('Blog Home', 'blog');
        $this->navigation->add_item('Blog Archive', 'blog/archive');
    }

    function index()
    {
        $data['title'] = 'Hello World!';
        $data['nav_menu'] = $this->navigation->get_all();
        $this->load->view('blog/home', $data);
        
    }
    
    function archive()
    {
        $data['title'] = 'Archive page';
        $data['nav_menu'] = $this->navigation->get_all();
        $this->load->view('blog/home', $data);
    }
}

Or you can extend the controller class:

./system/application/libraries/MY_Controller.php
Code:
<?php

class MY_Controller extends Controller {

    function MY_Controller()
    {
        parent::Controller(); # Call the parent constructor.
        $this->_set_navigation();
    }

    function _set_navigation() # prefix with an underscore so it's not accessible from the outside.
    {
        # This will be overriden by your controller.
    }
}

You're controllers would then need to extend MY_Controller, and you'd need to call parent::MY_Controller(); from within the constructor.


Messages In This Thread
Autoloading a set function in all the controllers - by El Forum - 03-30-2009, 06:36 AM
Autoloading a set function in all the controllers - by El Forum - 03-30-2009, 06:59 AM
Autoloading a set function in all the controllers - by El Forum - 03-30-2009, 07:28 AM
Autoloading a set function in all the controllers - by El Forum - 03-30-2009, 07:38 AM
Autoloading a set function in all the controllers - by El Forum - 03-30-2009, 07:47 AM
Autoloading a set function in all the controllers - by El Forum - 03-30-2009, 08:02 AM
Autoloading a set function in all the controllers - by El Forum - 03-30-2009, 08:32 AM
Autoloading a set function in all the controllers - by El Forum - 03-30-2009, 08:49 AM
Autoloading a set function in all the controllers - by El Forum - 03-30-2009, 09:09 AM
Autoloading a set function in all the controllers - by El Forum - 03-30-2009, 09:43 AM
Autoloading a set function in all the controllers - by El Forum - 03-30-2009, 10:00 AM
Autoloading a set function in all the controllers - by El Forum - 03-30-2009, 10:23 AM



Theme © iAndrew 2016 - Forum software by © MyBB