Welcome Guest, Not a member yet? Register   Sign In
New with Code Igniter - Application structure
#21

[eluser]crispinatari[/eluser]
[quote author="ayukawaa" date="1236264129"]This is well documented in the forums and it has been answered many times I think.

For me, instead of creating many controllers that extend the 'Controller' (repeating code in all the controllers), I create one MY_Controller library which extends Controller and I put in there all the common code.

Then, all the controllers must extend My_Controller, NOT Controller

For example, create your controllers like
Code:
class ControllerExample extends MY_Controller {
    
    function ControllerExample()
    {
        //this call the MY_CONTROLLER (parent) constructor
        parent::MY_Controller();
    }

    function index()
    {
        //generate one $config array with all the parts that are UNIQUE in this controller
        $data['var1'] = ...
        $data['var2'] = ...
        $data['var3'] = ...
        $config['main_content'] = $this->load->view('main_content', $data, true);
        $config['section1'] = $this->load->view('section1', $data, true);
        $config['section2'] = $this->load->view('section2', $data, true);
        $config['section3'] = $this->load->view('section3', $data, true);
        (...)    
        //End, screen output
        $this->output_screen( $config );
    }

    (...)
    
}
And create in application/libraries/MY_Controller.php:
Code:
class MY_Controller extends Controller{
    
    function MY_Controller()
    {
        //this call the parent constructor
        parent::Controller();
    }

    //ONLY ONE output point, pass all data through array
    function output_screen($config){
        //append to the $config array all the common data you need (menus, etc)
        $data['var1'] = ...
        $data['var2'] = ...
        $data['var3'] = ...
        $config['commonsection1'] = $this->load->view('commonsection1', $data, true);
        $config['commonsection2'] = $this->load->view('commonsection2', $data, true);
        $config['commonsection3'] = $this->load->view('commonsection3', $data, true);
        (...)
        //SCREEN OUTPUT (this should be the only one outputting to screen)
        $this->load->view('template_of_sections', $config);
    }

    (...)
}

And that's all.

^_^[/quote]

what code do you use in the model and view for your example though? is ControllerExample meant for Site_Template for instance.

this is doing my head in. and what does $data['var1'] = ... represent?


Messages In This Thread
New with Code Igniter - Application structure - by El Forum - 02-25-2009, 12:42 PM
New with Code Igniter - Application structure - by El Forum - 02-25-2009, 06:32 PM
New with Code Igniter - Application structure - by El Forum - 02-25-2009, 08:22 PM
New with Code Igniter - Application structure - by El Forum - 02-26-2009, 12:54 PM
New with Code Igniter - Application structure - by El Forum - 02-26-2009, 01:43 PM
New with Code Igniter - Application structure - by El Forum - 02-26-2009, 01:53 PM
New with Code Igniter - Application structure - by El Forum - 02-26-2009, 01:58 PM
New with Code Igniter - Application structure - by El Forum - 02-26-2009, 02:00 PM
New with Code Igniter - Application structure - by El Forum - 02-26-2009, 02:08 PM
New with Code Igniter - Application structure - by El Forum - 02-26-2009, 04:24 PM
New with Code Igniter - Application structure - by El Forum - 03-03-2009, 01:32 PM
New with Code Igniter - Application structure - by El Forum - 03-03-2009, 04:40 PM
New with Code Igniter - Application structure - by El Forum - 03-03-2009, 11:48 PM
New with Code Igniter - Application structure - by El Forum - 03-05-2009, 02:42 AM
New with Code Igniter - Application structure - by El Forum - 04-03-2009, 03:43 PM
New with Code Igniter - Application structure - by El Forum - 04-03-2009, 05:35 PM
New with Code Igniter - Application structure - by El Forum - 04-03-2009, 07:12 PM
New with Code Igniter - Application structure - by El Forum - 04-03-2009, 07:39 PM
New with Code Igniter - Application structure - by El Forum - 04-03-2009, 08:00 PM
New with Code Igniter - Application structure - by El Forum - 04-04-2009, 04:04 AM
New with Code Igniter - Application structure - by El Forum - 06-13-2009, 04:44 AM
New with Code Igniter - Application structure - by El Forum - 06-13-2009, 05:46 AM



Theme © iAndrew 2016 - Forum software by © MyBB