问题
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.
回答1:
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!!
回答2:
If you are loading the models in the parent class or in the construct then it should be inherited in shop_crud. are you not looking to do class Shop_crud extends Core_crud {
? is parent::__construct() not retaining the construct for you?
Is this something you can handle with routing to the same controller rather than extending a controller (wanting to inherit both the controller and the model seems strange to me or something you could handle with a route and a private function in the class to handle the logic)?
回答3:
"Controllers" this name defines it's functionality. The controller is used to control a particular section. So in MVC framework I think it's better to create individual controller for individual module. But you can reuse the model i.e. you can call one model's function in another model. For this
First load your model like $this->load->model("modelName"); in your controller
Then call the function like $this->modelname->functionName();
回答4:
From what I can gather, you have to require the parent controller that you are extending. This isn't exactly ideal, but I'll look into a better way to do this later on. For now, I've created a simple function to do the inclusion.
function extend_module($module) {
$path = realpath(APPPATH) . '/modules/'. $module.'/controllers/'.ucfirst($module).'.php';
require_once($path);
}
Usage:
extend_module('some_module');
class othe_ module extends some_module {
NOTE: The function needs to be available outside of the CI object, so put it somewhere like your main index.php file.
Also note: As these variables are used to reference the local file system, do not dynamically assign them directly from user generated input. Doing so would cause multiple file system vulnerabilities.
Platform: CI3 + Bonfire 8 HMVC
来源:https://stackoverflow.com/questions/15073721/extending-hmvc-modules-in-codeigniter