2011-08-06 30 views
3

我是新來的codeigniter和使用MVC構建Web應用程序。我試圖圍繞如何在應用程序中以模塊化方式實現小部件。我的問題在這一點上更加理論化。我沒有實際的代碼顯示。CodeIgniter:MVC和Widgets?

我想知道的是,我將如何構建一個數據驅動的小部件,以便我可以簡單地將其放到任何我想要的頁面上。例如,假設我有一個名爲Widget的小部件。我創建了一個名爲/models/widget_model.php的模型文件。然後我有一個名爲/controllers/widget.php的控制器文件。顯然,我的控制器將使用該模型從我的數據庫中獲取必要的數據。我不明白的是如何將它作爲一個小部件放到多個視圖上。我目前看到和理解的是如何使用控制器來驅動特定的視圖。所以基本上就像每個頁面使用一個控制器。我猜是以模塊化的方式使用這個小部件的過程是什麼?

回答

5

你要搜索的是HMVC。您可以使用兩種通用庫/軟件包:Modular CIHMVC。有了這個,你可以在你的視圖文件中加入類似<?php echo Modules::run('module/controller/method', $param, $...); ?>的東西作爲一個小部件。

+0

HMVC解決方案看起來很不錯。我現在正在研究它。 – LoneWolfPR

0

我想你可以簡單地使用CI的視圖系統。您爲每個小部件創建一個視圖,然後從模型中注入想要的任何變量,最後在任何需要的位置顯示結果HTML。我想不出有什麼特別的困難。

+0

糾正我,如果我錯了,但觀點不應該引用模型雖然。來自數據庫的信息應該由控制器從模型中抓取,然後傳遞給視圖。我想這最好的描述就像一個插件。 – LoneWolfPR

+0

你是對的,一個視圖不應該直接訪問模型 - 這就是爲什麼我寫了你需要**從模型中注入**變量的原因。 –

1

你可以通過驅動來完成。將控制器作爲對象引用發送給驅動程序以使用視圖類。然後你只需加載驅動程序並將它們用作插件。

編輯: 這是我在應用程序中使用的代碼:

<?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 */ 

負載類是有讓您使用視圖類和控制器有沒有讓你使用的數據庫功能,給你如果您需要,還可以使用其他一些訪此類需要在所有其他驅動程序(小部件)和所有驅動程序(小部件)需要擴展此類之前加載。你可以在application/config/autoload.php的$ config ['libraries']數組中添加'basedriver'。

示例驅動程序的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 */ 

要使用延伸Basedriver作爲窗口小部件,例如司機:

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

你有這個簡短的例子代碼嗎? –