php - 在 CodeIgniter 中扩展 HMVC 模块

标签 php codeigniter inheritance module hmvc

假设我们有一个名为 core_crud 的模块, 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
    }

}

现在我想用另一个名为 shop_crud 的模块来扩展这个模块。这个 shop_crud 模块的基本 Controller 是什么样子的?我的意思是我想从 core_crud 继承所有 Controller 方法以及所有模型内容。

最佳答案

模块结构

/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);
    }

}

因此,与其扩展整个模块/ Controller ,我更喜欢只调用所需的方法。它也很简单。

注意如果模块名称和 Controller 名称不同,则必须传递路径

module_name/controller_name/mymethod

编辑以支持扩展

文件结构

File Structure

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.');
    }

}

Output :- I am from controller core_crud. I am from model mdl_core_crud. Param is Working.

希望这对您有所帮助。谢谢!!

关于php - 在 CodeIgniter 中扩展 HMVC 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15073721/

相关文章:

php - 图片的替代文字没有出现

php - 在php中获取两个日期之间的周数

python - 一个Python类可以不继承多个类,而是有一个 "choice"的继承吗?

PHP 命名空间和继承

php - 设置 cron 作业 php

java - 不使用 super 的子类方法的默认行为是什么?

php - PDO-MySQL : Boolean values get converted to 1 or empty string on prepared statement binding

PHP 在第 31 个月的日期添加月份至今

php - Codeigniter 3插入查询失败

php - 获取给定日期的总数量和价格