Wednesday, March 28, 2012

Codeigniter + HMVC + Namespaces (part 2)

Now I'm going to continue with CI+NAMESPACES+HMVC transformation. (See part 1)

Codeigniter + Namespaces + HMVC

Working with namespaces in hmvc is same that part 1. The diference is how we load a namespaced module:

namespace controllers\blog;

class user extends \MX_Controller {
  public function get_comments($id_user){
     /* Code */

     $module = $this->load->module('namespace\modulename/classname');
     $module->method($x, $y);

     // OR [3]
     \Modules::run('namespace\modulename/classname/method', $x, $y);
$_ns = __NAMESPACE__;

How to load a module
  1. Load module into a variable. The format is namespace\[backslash]modulename/[slash]classname
  2. Call the module method
  3. Run module method statically
Download HMVC extension patch

Download HMVC module example


Suraj Kumar Adhikari said...

Can you please provide me a working copy of HMVC + Namespaces + CI... I've tried but didn't get any success. Please give a working copy

Cristian Riffo Huez said...

Hi Suraj. I've uploaded an example in this post. Search for 'Download HMVC module example' link.