在CodeIgniter中扩展HMVC模块 [英] Extending HMVC modules in CodeIgniter
问题描述
假设我们在控制器中有一个名为 core_crud
的模块:
if(!defined('BASEPATH'))
exit('不允许直接脚本访问');
class Core_crud extends MX_Controller
{
function __construct()
{
parent :: __ construct
$ this-> load-> model('mdl_core_crud');
}
public function index()
{
//代码在这里
}
}
现在我想使用另一个模块扩展这个模块 shop_crud
。这个 shop_crud
模块的基本控制器怎么样?我的意思是我想继承所有的控制器方法从 core_crud
和所有的模型的东西。
模块的结构
/ modules
/ core_crud
/ controllers
/core_crud.php
/ models
/ views
/ shop_curd
/ controllers
/shop_crud.php
/ models
/视图
代码 core_crud.php
p>
<?php
if(!defined('BASEPATH'))
exit 不允许直接脚本访问);
class Core_crud extends MX_Controller
{
function __construct()
{
parent :: __ construct
$ this-> load-> model('mdl_core_crud');
}
public function index()
{
//代码在这里
}
public function mymethod param1 ='',$ param2 ='')
{
return'Hello,I am called with paramaters'。 $ param1。 '和'。 $ param2;
}
}
shop_crud.php
<?php
if defined('BASEPATH'))
exit('不允许直接脚本访问);
class Shop_crud extends MX_Controller
{
public function __construct()
{
parent :: __ construct
// $ this-> load-> model('mdl_shop_curd');
}
public function testmethod()
{
//直接输出
$ this-> load-> controller('core_crud / mymethod ',array('hello','world'));
//捕获变量中的输出
$ myvar = $ this-> load-> controller('core_crud / mymethod',array('hello','world'),真正);
}
}
/ controller我更喜欢只是调用所需的方法。
注意如果模块名称和控制器名称不同,则必须传递路径 $ b$ b
module_name / controller_name / mymethod
编辑以支持EXTENDS
文件结构
core_crud.php
/ p>
if(!defined('BASEPATH'))
exit('No direct script access'
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'我来自控制器core_crud。 '。 $ this-> mdl_core_crud-> hello_model()。 'Param is'。 $ param1;
}
}
mdl_core_crud.php
if(!defined('BASEPATH'))
exit('No direct script access allowed');
类mdl_core_crud扩展CI_Model
{
public function hello_model()
{
return'我来自模型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。 Param正在工作。
希望这有帮助。谢谢!
Let's say we have module called core_crud
with something like this in the controller:
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
}
}
And now I want to extend this module with another module called shop_crud
. How would the basic controller for this shop_crud
module look like? I mean I want to inherit all the controller methods from core_crud
and all the model stuff too.
Structure of the Modules
/modules
/core_crud
/controllers
/core_crud.php
/models
/views
/shop_curd
/controllers
/shop_crud.php
/models
/views
Code in 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;
}
}
Code in 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);
}
}
So instead of extending the whole module/controller I prefer just to call the method which is required. It is simple and easy too.
Note If module name and controller name are different then you have to pass the path
module_name/controller_name/mymethod
EDIT to support EXTENDS
File structure
The code in 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;
}
}
The code in 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.';
}
}
The code in 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.');
}
}
Output :- I am from controller core_crud. I am from model mdl_core_crud. Param is Working.
Hope this helps. Thanks!!
这篇关于在CodeIgniter中扩展HMVC模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!