首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CodeIgniter: MVC和Widgets?

CodeIgniter: MVC和Widgets?
EN

Stack Overflow用户
提问于 2011-08-06 23:13:36
回答 3查看 2.2K关注 0票数 3

我对codeigniter和使用MVC构建web应用程序是个新手。我正在努力思考如何在我的应用程序中以模块化方式实现小部件。在这一点上,我的问题更多是理论上的。我没有实际的代码可以展示。

我想知道的是,我如何构建一个数据驱动的小部件,这样我就可以简单地将它放到我想要的任何页面上。例如,假设我有一个名为Widget的小部件。我已经创建了一个名为/models/widget_model.php的模型文件。然后我有一个名为/controllers/widget.php的控制器文件。显然,我的控制器将使用该模型从我的数据库中获取必要的数据。我不明白的是,如何将其用作拖放到多个视图上的小部件。到目前为止,我所看到和理解的是如何使用控制器来驱动特定的视图。所以它基本上就像是每个页面使用一个控制器。我想以模块化的方式使用这个小部件的过程是什么?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-08-06 23:47:57

您要搜索的是HMVC。您可以使用两个通用库/包:Modular CIHMVC。这样,您就可以将诸如<?php echo Modules::run('module/controller/method', $param, $...); ?>之类的内容作为小部件放入视图文件中。

票数 5
EN

Stack Overflow用户

发布于 2012-12-08 05:32:26

您可以通过驱动程序完成此操作。将控制器作为对象引用发送给驱动程序以使用视图类。然后,您只需加载驱动程序并将其用作插件。

编辑:以下是我在应用程序中使用的代码:

代码语言:javascript
复制
<?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类允许您使用视图类,而controller允许您使用数据库函数,并在需要时为您提供其他访问权限。在所有其他驱动程序(窗口小部件)和所有驱动程序(窗口小部件)需要扩展此类之前,需要加载此类。您可以通过在application/config/autoload.php中的$config‘’libraries‘数组中添加'basedriver’来完成此操作。

驱动程序小部件示例:

代码语言:javascript
复制
    <?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 */

要将扩展Basedriver的驱动程序用作小部件,请执行以下示例:

代码语言:javascript
复制
$this->load->driver('example');
$this->example->render(array('parameter1' => '1', 'parameter2' => '2'));
票数 1
EN

Stack Overflow用户

发布于 2011-08-06 23:20:19

我认为你可以简单地使用CI的视图系统。您为每个小部件创建一个视图,然后从模型中注入所需的任何变量,最后将生成的HTML显示在所需的任何位置。我想不出有什么特别的困难。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6967721

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档