简体   繁体   English

在CodeIgniter中扩展HMVC模块

[英]Extending HMVC modules in CodeIgniter

Let's say we have module called core_crud with something like this in the controller: 假设我们在控制器中有一个名为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
    }

}

And now I want to extend this module with another module called shop_crud . 现在我想用另一个名为shop_crud模块扩展这个模块。 How would the basic controller for this shop_crud module look like? 这个shop_crud模块的基本控制器怎么样? I mean I want to inherit all the controller methods from core_crud and all the model stuff too. 我的意思是我想继承core_crud和所有模型的所有控制器方法。

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 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 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 编辑以支持EXTENDS

File structure 文件结构

文件结构

The code in core_crud.php . 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 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 . 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. 输出: - 我来自控制器core_crud。 I am from model mdl_core_crud. 我来自模特mdl_core_crud。 Param is Working. 帕拉姆正在工作。

Hope this helps. 希望这可以帮助。 Thanks!! 谢谢!!

If you are loading the models in the parent class or in the construct then it should be inherited in shop_crud. 如果要在父类或构造中加载模型,那么它应该在shop_crud中继承。 are you not looking to do class Shop_crud extends Core_crud { ? 你不打算class Shop_crud extends Core_crud {吗? class Shop_crud extends Core_crud { is parent::__construct() not retaining the construct for you? 是父_ ::_ construct()不保留你的构造?

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)? 这是你可以通过路由到同一个控制器而不是扩展控制器来处理的东西(想要继承控制器和模型对我来说似乎很奇怪,或者你可以处理类中的路由和私有函数来处理逻辑)?

"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. 所以在MVC框架中我认为最好为单个模块创建单独的控制器。 But you can reuse the model ie 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();

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. 注意:该函数需要在CI对象之外可用,因此将其放在主index.php文件之类的位置。

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 平台:CI3 + Bonfire 8 HMVC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM