Welcome Guest, Not a member yet? Register   Sign In
Codeigniter HMVC bug ?
#1

[eluser]r4f[/eluser]
Hello there

I have a question, whether this is a bug or perhaps I'm doing something wrong....

I use the CI 2.1 with HMVC 5.3. Everything works out as a charm, until I do this:

I auto-load a model, that runs through all the modules and runs them one by one like this: Modules::run('module_name');
each of the modules then gets initiated and the code inside the index() function runs.
Everything works okay until I write in the addressbar (example module "Gallery): /gallery/load/1

The gallery.php (/modules/gallery/controllers/gallery.php) file looks like this:
Code:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Gallery extends MX_Controller {
    public function index()
    {
        // this works when I auto-load a model and run this from the model with "Modules::run('gallery');"
        Modules::run("module/function", "param");
    }

    public function load($main_gallery, $sub_gallery = 0, $image = NULL) {
        // LOAD THE GALLERIES!

       print "what the...";
       exit;
    }
}

The error I get is:
Fatal error: Class 'Gallery' not found in /Applications/XAMPP/xamppfiles/www/DOMAIN/system/application/third_party/MX/Modules.php on line 105

I tried playing around inside the Modules.php file and it seems that the file gets included, but for some reason the class can't be found!!!
If I move the file inside /system/application/controllers/gallery.php then it works! It's a problem with the MX I think.

Please help.... is this a bug or...?
#2

[eluser]InsiteFX[/eluser]
Try adding your __constructor
#3

[eluser]wiredesignz[/eluser]
@r4f, Your code works as it should in my test environment when called from the URL. I have no idea what you are trying to achieve with the code in the index method.
#4

[eluser]r4f[/eluser]
Okay I will try to explain it here the best I can:
I have a small custom CMS system, where we use it with MX to include different kind of modules.

The auto-load model includes this code in the __construct method:
Code:
// load the modules
$this->load->model('modules_model');

This loads the modules_model model and runs the __construct method which is this:
Code:
class Modules_model extends CI_Model
{    
    public function __construct() {
        parent::__construct();
        
            //load all the active modules from the database
        $query = $this->db->get_where('cms_modules', array('active' => 1));
        foreach ($query->result() as $row)
        {
            $m_path =  realpath(MODULES_PATH);
                // if dir exists, load
            if(is_dir($m_path . "/" . $row->name)) {
                if(file_exists(MODULES_PATH . $row->name . '/info.php')) {
                    Modules::run($row->name);
                }
            }
        }        
    }
}

One of the rows from DB is for example "gallery". So it for example looks like this "Modules::run('gallery')".

Inside the gallery controller:
Code:
class Gallery extends MX_Controller {
    public function index()
    {
        // add this to admin mainmenu
        Modules::run("cms_extra/add_to_admin_bar", "Gallery", urlencode(base_url()."cms/admin/gallery/1/view_admin_gallery"), 60);
    }
}

The error that comes up says that class Gallery is not found.

The controller is in /modules/gallery/controllers/
The other files mentioned are in /system/application/models/ folder.

If you can get this to work then something is wrong with my setup.....
#5

[eluser]wiredesignz[/eluser]
Your use of Modular Extensions looks rather whacked. But make sure the controller which loads the model that uses HMVC also extends the MX_Controller class.

@InsiteFX, FYI, empty constructors are not required in a php5. The parent constructor will be called by default.
#6

[eluser]r4f[/eluser]
There is no controller in the beginning, since the model is loaded automatically, defined in autoload.php.

Well, I might have to change the structure, you're right about that, but I still think it's a bug.
One should be able to run Modules::run() redundantly... for example a model that loads another model, that loads another model, and uses Modules::run, that again uses Modules::run, and again.... and it should also work if I step into the controller from the browsers address bar. It's a really weird bug, because I use Modules::run in an autoloaded model. And that works fine, but if I enter one of the loaded controllers from the browser, it cannot find the class. I will try to dig deeper into this matter and see if I can locate a specific error/bug in the code, and post it here.

I still think MX is an fantastic piece of work that perhaps should be a standard part of CI. Smile
#7

[eluser]wiredesignz[/eluser]
[quote author="r4f" date="1340622248"]There is no controller in the beginning, since the model is loaded automatically, defined in autoload.php...
[/quote]

In fact there is a controller in action because the autoload sequence is initiated in the controller constructor.

Modules::run was never intended to be used from a model. The HMVC design pattern focuses on controller to controller communications.




Theme © iAndrew 2016 - Forum software by © MyBB