Paulo
Paulo

Reputation: 77

extend controller inside HMVC Codeigniter module

How do I extend a controller of a HMVC module within the module itself?

class Backend extends Backend_Controller {
    public function __construct(){
        parent::__construct();
    }
}

Assuming the following typical Codeigniter file structure as it relates to HMVC:

/
/application
/application/modules
/application/modules/backen
/application/modules/backen/controllers
/application/modules/backen/controllers/Backend.php
/application/modules/backen/libraries
/application/modules/backen/libraries/Backend_Controller.php

In this structure get the error "class not found". Works to put in the folder "/application/libraries/Backend_Controller.php".

Upvotes: 0

Views: 1646

Answers (2)

umefarooq
umefarooq

Reputation: 4574

Hi CI always look for core classes starts with CI_Controller or extended classes name started with MY_ like MY_Controller MY_Email etc if your any other class to be called not like library you can add the following code in config.php will autoload custom class

/*
| -------------------------------------------------------------------------
| Native spl_autoload_register() - by Kenneth Vogt
| -------------------------------------------------------------------------
|
| Here is an updated version of Phil Sturgeon’s code:
|
| Thanks to Phil Sturgeon Kenneth Vogt and InsiteFX.
|
| NOTE:
| Requires PHP 5.3.+
| As of CI 3.0 Dev - The constant EXT has been removed modified
| to use '.php' now instead of EXT.
| should work for all version of CI and PHP 5.3
|
| Place at the bottom of your ./application/config/config.php file.
| -------------------------------------------------------------------------
*/

spl_autoload_register(function($class)
{
    if (strpos($class, 'CI_') !== 0)
    {
        if (file_exists($file = APPPATH . 'core/' . $class . '.php'))
        {
            include $file;
        }
        elseif (file_exists($file = APPPATH . 'libraries/' . $class . '.php'))
        {
            include $file;
        }
    }
}); 

reference from this fourm post thread http://forum.codeigniter.com/thread-473-post-2679.html#pid2679

now you can extend your controller with custom controller name Backend_Controller this class should available under application either library or core directory

Upvotes: 0

Bart Heideman
Bart Heideman

Reputation: 71

Controllers have to extend CI_Controller in CodeIgniter. Controllers cannot extend libraries but they can include them like this $this->load->library('backendLib');

If you are using Wiredesignz HMVC extension you can use base controllers for this. Just make a backend_controller class in the core directory and make it extend MX_Controller. Now you can make the module controller extend the backend_controller.

Best,

Bart

Upvotes: 2

Related Questions