Multiple controllers simultaneously?

#1
[eluser]Unknown[/eluser]
Let's say I have two controlles: 'news' and 'polls'.
On the page that displays news, I also want a box with a poll.,
Or let's say I always want a 'web_links' controller displayed on the bottom of every page?

Does CI have a procedure for this? Or MVC frameworks in general?

Should I instantiate two controllers? Or is there a preferable method?

Thanks in advance Smile

#2
[eluser]coolfactor[/eluser]
In CI, controllers are used to display pages, not parts of pages (unless it's an ajax request). A controller is actually just a special library. You either want to create a regular library or model class to handle your "news", "polls", and "web_links" components, and then any controller can utilize them to build a page.

#3
[eluser]esra[/eluser]
Coolfactor summarizes the overall problem.

You need to add third party support for blocks in order to make this work. A block would be an entity that is loaded in a template (or master view) with its own dedicated controller and model code. There are numerous threads about creating blocks using a partial view in combination with a helper. The helper handles the controller and business logic (model) code.

This actually works very well with Coolfactor's proposed View library for CI in combination with some block support ideas discussed in the Modular Separation thread and other threads. CI and the View library make no distinction between the various flavors of views that might exist. To treat blocks as separate entities in CI, additional search paths are added to an extended version of the Loader library and the view method is overloaded to search additional paths for other flavors of views such as blocks, components (wrapped form elements, trees, accordians, grids, etc.) or templates. The distinction between various view types is made based on where the view is stored. The caveat is that all views, regardless of their flavor, need unique names. Below is an overloaded view method for modular separation v1.0 (will not work with 2.x yet) that allows module views, block views, and templates to be loaded from separate directory structures.

Code:
function view($view, $vars = array(), $return = false)
    {
        $module = '';
        $CI = &get;_instance();

        // Is the requested view a module view?
        if (is_dir(APPPATH . 'modules/' . $CI->uri->segment(1))) {
            $module = $CI->uri->segment(1);
        }

        // Is the requested view a module view in a sub-folder?
        if (is_dir(APPPATH . 'modules/' . $CI->uri->segment(2) . '/modules/' . $CI->uri->segment(1))) {
            $module = $CI->uri->segment(2);
        }

        // Make the request a module view
        if (file_exists(APPPATH . 'modules/' . $module . '/views/' . $view . EXT)) {
            $view = '../modules/' . $module . '/views/' . $view;
        }

        // Make the request a template view
        if (file_exists(APPPATH . 'templates/' . $view . '/' . $view . EXT)) {
            $view = '../templates/' . $view . '/' . $view;
        }

        // Make the request a block view
        if (file_exists(APPPATH . 'blocks/' . $view . '/' . $view . EXT)) {
            $view = '../blocks/' . $view . '/' . $view;
        }

        return parent::view($view, $vars, $return);
    }

If you don't need modular separation (CMF-like infrastructure), the above approach can be modified to use blocks with the standard MVC directory structure. With the above, you would add three new directories to application/ called modules, blocks, and templates. Support for the original MVC directory structure remains, so controllers, models, and views could be loaded like normal if they were not stored in the three new directory hierarchies.

The above approach is not the only approach to handling this problem. There are other solutions discussed in various threads. I believe that Rick Jolly started the most recent thread on this topic a few weeks ago.

Fairly shortly, you should see a better approach based on the use of a Block controller in conjunction with an extended version of CF's View library and the Modular Separation extensions. It should be possible to modify that approach to work with the standard MVC directory structure. This approach allows the module, block, and template entity names and directory names to be changed from a config file. Once the current refactoring of modular separation v2.x is completed, you should see a solution shortly afterwards.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.