Welcome Guest, Not a member yet? Register   Sign In
Controller Code Reuse (partial solution).
#1

[eluser]MPress[/eluser]
Hello. I just started looking at CI yesterday and I almost immediately took notice to the fact that you cannot load a controller within another controller. So I started modifying the core today and got pretty far with it. Eventually, after enough modifications, I noticed that all autoloads began loading twice and I started receiving the associated fatal errors for anything that required instantiation. The problem lies in the fact that the constructor for the Controller class is what invokes the autoload calls. Therefore, upon instantiating a second controller class, all of the calls to include/instantiate all autoloads in the autoload.php file were made for a second time.

So I scrapped that idea and came up with a simple partial solution that allows one to at least share controller logic in the application scope.

First make one simple modification to CodeIgniter.php:
Code:
load_class('Application_controller', FALSE);
... around line 131, right after the regular controller is loaded.

Then in the 'system/application/libraries' directory, create an Application_controller.php file:
Code:
class ApplicationController extends Controller{

    function ApplicationController(){
        
        parent::Controller();
    }
}

Now all of your controllers should extend ApplicationController.


Example usage:
Code:
class Home extends ApplicationController{

    function Home(){
        
        parent::ApplicationController();
    }
    
    function index(){
        
        $this->render('home', 'Title for home page.');
    }
}
Code:
class ApplicationController extends Controller{

    function ApplicationController(){
        
        parent::Controller();
    }
    
    function render($view, $title = '', $show_side_bar = true){
    
        $data = array();
        $data['content'] = $this->load->view($view, '', true);
        $data['title'] = $title;
        
        if($show_side_bar){
            
            $sidebar_data = array();
            #.....etc.
            
            $data['side_bar'] = $this->load->view('side_bar', $sidebar_data, true);
        }
        
        $this->load->view('layout', $data);
    }
}


Messages In This Thread
Controller Code Reuse (partial solution). - by El Forum - 09-28-2007, 05:16 AM
Controller Code Reuse (partial solution). - by El Forum - 09-28-2007, 07:20 AM
Controller Code Reuse (partial solution). - by El Forum - 09-28-2007, 10:44 AM
Controller Code Reuse (partial solution). - by El Forum - 09-29-2007, 06:49 PM
Controller Code Reuse (partial solution). - by El Forum - 09-29-2007, 07:29 PM
Controller Code Reuse (partial solution). - by El Forum - 09-29-2007, 08:01 PM
Controller Code Reuse (partial solution). - by El Forum - 09-29-2007, 08:53 PM
Controller Code Reuse (partial solution). - by El Forum - 09-29-2007, 10:12 PM
Controller Code Reuse (partial solution). - by El Forum - 09-29-2007, 11:03 PM



Theme © iAndrew 2016 - Forum software by © MyBB