你可以通過驅動來完成。將控制器作爲對象引用發送給驅動程序以使用視圖類。然後你只需加載驅動程序並將它們用作插件。
編輯: 這是我在應用程序中使用的代碼:
<?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'));
HMVC解決方案看起來很不錯。我現在正在研究它。 – LoneWolfPR