[英]Extending HMVC modules in CodeIgniter
假設我們在控制器中有一個名為core_crud
模塊:
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
}
}
現在我想用另一個名為shop_crud
模塊擴展這個模塊。 這個shop_crud
模塊的基本控制器怎么樣? 我的意思是我想繼承core_crud
和所有模型的所有控制器方法。
模塊的結構
/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
編輯以支持EXTENDS
文件結構
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。 帕拉姆正在工作。
希望這可以幫助。 謝謝!!
如果要在父類或構造中加載模型,那么它應該在shop_crud中繼承。 你不打算class Shop_crud extends Core_crud {
嗎? class Shop_crud extends Core_crud {
? 是父_ ::_ construct()不保留你的構造?
這是你可以通過路由到同一個控制器而不是擴展控制器來處理的東西(想要繼承控制器和模型對我來說似乎很奇怪,或者你可以處理類中的路由和私有函數來處理邏輯)?
“控制器”這個名稱定義了它的功能。 控制器用於控制特定部分。 所以在MVC框架中我認為最好為單個模塊創建單獨的控制器。 但是你可以重用模型,即你可以在另一個模型中調用一個模型的函數。 為了這
First load your model like $this->load->model("modelName"); in your controller
Then call the function like $this->modelname->functionName();
根據我的收集,您必須要求您正在擴展的父控制器。 這並不完全理想,但我會在以后研究一種更好的方法。 現在,我已經創建了一個簡單的函數來進行包含。
function extend_module($module) {
$path = realpath(APPPATH) . '/modules/'. $module.'/controllers/'.ucfirst($module).'.php';
require_once($path);
}
extend_module('some_module');
class othe_ module extends some_module {
注意:該函數需要在CI對象之外可用,因此將其放在主index.php文件之類的位置。
另請注意:由於這些變量用於引用本地文件系統,因此請勿直接從用戶生成的輸入中動態分配它們。 這樣做會導致多個文件系統漏洞。
平台:CI3 + Bonfire 8 HMVC
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.