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?
#22

[eluser]jedd[/eluser]
[quote author="crispinatari" date="1244907857"]
what code do you use in the model and view for your example though? is ControllerExample meant for Site_Template for instance.
[/quote]

That's a huge question. Model code will be doing modelly things, such as reading or writing to a database.

I can't understand your second question there.

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

The first variable that you might want to send to your view. var2 - the second. And so on.

They'll appear in the view as $var1, $var2 .. and so on.

Have you really read the CI User Guide - getting started, and the Model, View and Controller sections?




Theme © iAndrew 2016 - Forum software by © MyBB