Welcome Guest, Not a member yet? Register   Sign In
What is the best way to achieve modular separation or HMVC in CI3?
#1
Star 

I know this topic is already beaten to death at StackOverflow, Reddit and even CI forums, but I was wondering if there was a better solution to this in 2016. I've been using CI3 for small and big web-apps for my clients and it had worked well until now. Presently, however, I want to build a very complex CRM app and its most important feature is extensibility.

In CI, we can define a bunch of CI_Controller derived classes in controllers folder that run based on the url path and views can be called from the views folder. But what I want is that my system should have modules and each module should have its own Controller and View. For example, the user will type something like these:

    http://localhost/modules/contacts
    http://localhost/modules/leads/add
    http://localhost/modules/sales

However, the contacts, sales and leads should not be predefined CI classes in my app. They should be something like Drupal modules or Wordpress plugins that the user can add/remove from the admin dashboard dynamically. The user or their developer should define their own controllers and views inside /modules/contacts/controllers and /modules/contacts/views folders. After some googling I found this CI modular-extensions-plugin that seems to do the job, but how good is it? The last I heard, plugin support was removed from CI3.

If its not easily possible in CI, then is it easily possible in any of the other frameworks like Laravel or Symfony?
Reply
#2

(This post was last modified: 05-31-2016, 10:25 AM by spjonez.)

Overload the loader (application/core/MY_Loader.php) to change the path of view files:

Code:
public function view( $view, $vars = array( ), $return = false ) {
    $ci =& get_instance( );

    if ( strpos( $view, '/' ) === false ) {
        $view = VIEWPATH . $ci->router->directory . 'views/' . $view;
    }

    if ( strpos( $view, '.' ) === false ) {
        $view .= '.html';
    }

    return $this->_ci_load( array( '_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array( $vars ), '_ci_return' => $return ) );
}
Reply
#3

You can change the view path in the index.php file to your own.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

Wiredesignz' HMVC works fine with CI3, as long as you use the CI3 version (which is what you've linked), and you extend the MX_Controller in your base controller (or in all of your controllers if you don't have a base controller). The controllers created in any modules would also have to extend MX_Controller or another controller which extends MX_Controller.

By default, it routes http://localhost/contacts to /application/modules/contacts/controllers/Contacts or /application/controllers/Contacts (it will search the paths in that order). If you wanted it to only route to the modules directory when you used http://localhost/modules, you'd have to modify the MX_Router.
Reply
#5

here is your answer.

<?php    
class Xyz extends MX_Controller
{
   $autoload = array(
       'helper'    => array('url', 'form'),
       'libraries' => array('email'),
   );
}
Reply




Theme © iAndrew 2016 - Forum software by © MyBB