模块结构
/modules
/core_crud
/controllers
/core_crud.php
/models
/views
/shop_curd
/controllers
/shop_crud.php
/models
/views
代码在core_crud.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Core_crud extends MX_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('mdl_core_crud');
}
public function index()
{
// code goes here
}
public function mymethod($param1 = '', $param2 = '')
{
return 'Hello, I am called with paramaters' . $param1 . ' and ' . $param2;
}
}
代码在shop_crud.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Shop_crud extends MX_Controller
{
public function __construct()
{
parent::__construct();
//$this->load->model('mdl_shop_curd');
}
public function testmethod()
{
// output directly
$this->load->controller('core_crud/mymethod', array('hello', 'world'));
// capture the output in variables
$myvar = $this->load->controller('core_crud/mymethod', array('hello', 'world'), TRUE);
}
}
因此,我宁愿只调用所需的方法,而不是扩展整个模块/控制器。它也很简单。
注意如果模块名称和控制器名称不同,则必须传递路径
module_name/controller_name/mymethod
编辑以支持扩展
文件结构
中的代码core_crud.php
。
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Core_crud extends MX_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('core_crud/mdl_core_crud');
}
public function index()
{
return 'index';
}
public function check_method($param1 = '')
{
return 'I am from controller core_crud. ' . $this->mdl_core_crud->hello_model() . ' Param is ' . $param1;
}
}
中的代码mdl_core_crud.php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class mdl_core_crud extends CI_Model
{
public function hello_model()
{
return 'I am from model mdl_core_crud.';
}
}
中的代码shop_crud.php
。
if (!defined('BASEPATH'))
exit('No direct script access allowed');
include_once APPPATH . '/modules/core_crud/controllers/core_crud.php';
class Shop_crud extends Core_crud
{
public function __construct()
{
parent::__construct();
}
public function index()
{
echo parent::check_method('Working.');
}
}
输出:- 我来自控制器 core_crud。我来自模型 mdl_core_crud。参数正在工作。
希望这可以帮助。谢谢!!