Welcome Guest, Not a member yet? Register   Sign In
Several controllers attached to a view
#1

[eluser]Trankh1[/eluser]
Hi,

I want to make my CodeIgniter application modular and for that, my views are organized with "micro-views" .

For example:

For the homePage view,

Code:
<?php

$this->load->view('includes/view1');

$this->load->view('includes/view2');

$this->load->view('includes/view3'); ?>


Each micro-view can be called into multiple views.

It means that, for the user view,

The view can be organized as :

Code:
<?php

$this->load->view('includes/view2');  // the same micro view as homepage

?>

For modularity reason (data loading and display), I want to attach a controller for each micro-view.
Thus, if i want to move, delete or add a micro-view into a view, I just need to modify my micro-view instead of my controller.

It means, that inside my micro-view, I call my attached controller in order to load data.
And if i want to use a micro view into an other view, i don't need to update the controller of the other view. I just need to update the other view file adding this line:

Code:
$this->load->view('includes/view2');


What do you think of that? It can save me a lot of time.
#2

[eluser]WanWizard[/eluser]
CI by default does not support controllers calling controllers.
You need modular support, for example using Modular CI or Modular Extensions HMVC.

My applications are completely modular, all requests route to the same bootstrap controller, which only purpose is to load the correct template (I use database routing to determine what should be on the page), call the module controllers to process the widgets (your micro views) on the page, and render the template.
#3

[eluser]Trankh1[/eluser]
I don't want to call controllers from other controllers.
I want to call controller from micro view so that my application is more modulable.
#4

[eluser]WanWizard[/eluser]
A view is the end product of a controller/method call, views should not call code. Besides that, you can't call controllers in (default) CI.

What I mean is, suppose you have a page template like this:
Code:
<html>
    <head>
        <title><?php echo $title; ?></title>
    <head>
    <body>
        <div id="header">&lt;?php echo $header;</div>
        <div id="body">&lt;?php echo $body;</div>
        <div id="footer">&lt;?php echo $footer;</div>
    &lt;/body&gt;
&lt;/html&gt;

The controller code is then
Code:
class Test {

    $data['title'] = 'page title';

    // fetchmodule is your preferred method of calling a module controller
    // the page layout (which modules to fetch for which section) should come from the database

    $data['header'] = $this->fetchmodule('module1/controller2/method3');
    $data['body'] = $this->fetchmodule('module4/controller5/method6');
    $data['footer'] = $this->fetchmodule('moduleA/controllerB/methodC');

    $this->load->view('pagetemplate', $data);
}

Ofcourse you would want to break it up even more, and have separate widgets for menu's, sidebars, different content types, etc...

This way, every distinct section of your pages is completely self contained within a module, making re-use extremely easy. New application? Reuse the application directory (doesn't contain any app specific items), grab the required modules, and off you go. You only have to write the content modules specific to that new application.
#5

[eluser]Trankh1[/eluser]
Thanks, does your proposition with "fetchmodule" work with native CodeIgniter?

I have downloaded ModularCI but it seems a big change when you have coded with native CI, right?
#6

[eluser]WanWizard[/eluser]
No, native CI can't load other controllers.

Modular CI follows the rules of CI, so you call a module controller method using $this->module->controller->method($vars), just like you're used to. You can even load the application folder as a module, so you can use this method to call other application controllers.




Theme © iAndrew 2016 - Forum software by © MyBB