Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate 3rd party modules

Tags:

codeigniter

I going to start new Codeigniter application and trying to find a way to separate 3rd party modules source (for example Tank Auth) from my own code, What i need is to setup a file tree like the following:

  • /project_root/
    • /system (framework system - done)
    • /3rd_party/
      • /dists/
      • /application/ (installed 3d party libraries/modules - the question topic)
    • /application/ ( my application - done)
    • /site/ (site root, working tree - done)

The /system /application /site done using index.php and application/config/config.php settings.

Is there any correct way to acheive the tree like above using configuration settings ? I new for Codeigniter and have no idea if such is even possible.

like image 440
zb' Avatar asked Sep 14 '26 14:09

zb'


1 Answers

For a modular structure with Codeigniter, Modular Extensions - HMVC is the go-to solution:

https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc

For your setup, you would add this to your config.php:

$config['modules_locations'] = array(
    // absolute path                    relative path
    APPPATH.'3rd_party/application/' => '../../3rd_party/application/',
);
  • /project_root/
    • /system
    • /3rd_party/
      • /dists/
      • /application/
        • tank_auth
        • my_module
        • some_other_third_party_code
    • /application/
    • /site/

The "relative path" is relative to any sub directory in your application root, like models, controllers, helpers, etc. For example in CI you can load a "model" from a "view" directory if you use:

$this->load->model('../views/some_model');

...not that's it's something you would normally do - but this is how HMVC's loader works. HVMC can be used to load anything CI normally can but from a "module" directory. You can have as many different modules or module paths as you want.

Modules can (but don't have to) load controller and act like their own mini-application. You can also load dependencies cross-module, or from the default application root. You can still use your default application directory alongside modules. To specify that you want to load a particular asset from a module path, just include the module name in the path:

// Loads the "tank_auth" library (works from the tank_auth module)
$this->load->library('tank_auth');

// Loads the "tank_auth" library (works from anywhere in your application)
$this->load->library('tank_auth/tank_auth');

This works with models, helpers, etc. as well.

To access a controller in tank_auth/controllers/login you would use the URL http://example.com/tank_auth/login. You can also run a controller within another controller by using the modules::run() method, which could allow you to "wrap" controller methods of modules in your own logic without touching them:

class my_login extends MX_Controller {
    function index() {
        modules::run('tank_auth/login');
    }
}

It's pretty well documented and has been around for years. I have used HMVC in nearly every Codeigniter project I've ever done, I highly recommend it.

like image 88
Wesley Murch Avatar answered Sep 17 '26 16:09

Wesley Murch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!