开发者

CodeIgniter: MVC and Widgets?

I'm new to codeigniter and building web applications using MVC. I'm trying to wrap my head around how I would implement widgets in a modular fashion in my application. My question is more theoretical at this point. I don't have actual code to show.

What I want to know is this, how would I construct a data-driven widget in such a way that I can simply开发者_运维百科 drop it on to any page that I want. For example, let's say I have a widget called Widget. I've created a model file called /models/widget_model.php. I then have a controller file called /controllers/widget.php. Obviously my controller will use the model to grab necessary data from my database. What I don't understand is how to use this as a widget dropped onto multiple views. What I'm seeing and understand so far is how to use a controller to drive a specific view. So it's basically like one controller is used per page. What would be the process of using this widget in a modular fashion I guess?


What you search for is HMVC. There are two common library/packages you can use : Modular CI or HMVC. With that, you can actually put something like <?php echo Modules::run('module/controller/method', $param, $...); ?> as a widget, in your view files.


You can do it via drivers. Send the controller as an object reference to the driver to use view class. Then you just load drivers and use them as plugins.

Edit: Here is the code I use in my application:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * CodeIgniter base widget driver
 * 
 * @author Alex
 * @version 1.0.0
 */
class Basedriver {

    /**
     * Current specified controller.
     * @var CI_Controller
     */
    public $controller;

    /**
     * Contents of the driver which should be outputted or returned.
     * @var string
     */
    protected $contents;

    /**
     * Loader Class
     * @var CI_Loader
     */
    protected $load;

    /**
     * Constructor function for Basedriver class
     */
    public function __construct()
    {
        $this->controller =& get_instance(); 
        $this->load = $this->controller->load;
    }

    /**
     * Renders driver data into specified output. If $echo_contents is true,
     * output is echoed to the client, otherwise it is returned.
     * @param boolean $echo_contents Specifies whether the content should be outputted or returned as string
     * @param mixed $params Array of parameters which should be sent to the driver
     * @return string Returned driver data if $echo_contents is set
     */
    public function render($params = NULL, $echo_contents = true)
    {
        $this->parse_params($params);
        $this->run();

        if ($echo_contents)
            echo $this->contents;
        else
            return $this->contents;

        return NULL;
    }

    /**
     * Default run function for all drivers, should be overidden by extending classes.
     */
    protected function run()
    {
        $this->contents = NULL;
    }

    /**
     * Parses parameters and sets them as variables.
     * Default variables need to be defined in extending class
     */
    protected function parse_params($params)
    {
        if ($params === NULL) return;
        foreach($params as $variable => $value)
        {
            if (isset($this->$variable))
                $this->$variable = $value;
        }
    }

}

/* End of file Basedriver.php */
/* Location: ./application/libraries/Basedriver.php */

Load class is there to allow you to use view class and controller is there to allow you to use database functions and to give you some other access if you need it. This class needs to be loaded before all other drivers (widgets) and all drivers (widgets) need to extend this class. You can do this by adding 'basedriver' in $config['libraries'] array in application/config/autoload.php.

Example Driver Widget:

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Example extends Basedriver
    {
       protected $parameter1 = 'defaultvalueparam1';
       protected $parameter2 = 'defaultvalueparam2';

       protected function run()
       {
          // Widget logic here...
          // you can use $this->load->view and $this->controller->db here
          $this->contents = 'final_processed_data_here';
       }
    }

    /* End of file Example.php */
    /* Location: ./application/libraries/Example/Example.php */

To use the driver which extends Basedriver as a widget, example:

$this->load->driver('example');
$this->example->render(array('parameter1' => '1', 'parameter2' => '2'));


I think you could simply using CI's view system. You create a view per widget, then you inject any variable you want from your model, and finally you display the resulting HTML anywhere you want. I can't think of any particular difficulty.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜