简体   繁体   中英

Codeigniter: Error in loading HMVC sub module model

I am having trouble loading model in sub module controller from sub module itself.

I am having few modules and sub-modules as below

modules/admin/
modules/admin/models
modules/admin/controllers
modules/admin/views

modules/admin/models/dashboard/
modules/admin/controllers/dashboard/
modules/admin/views/dashboard/

modules/admin/models/plugs/
modules/admin/controllers/plugs/
modules/admin/views/plugs/

Each M/V/C has own files in it.

Now I have created model in modules/admin/models/plugs/ just for testing purpose something like below

Plugs Model

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Plugs extends CI_Model
{
    public function __construct()
    {
        parent::__construct();    

    }


    public function mymeta()
    {
        return 'plugs model loded';
    }

}

And now trying to load into Plugs Controller as below

<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class Plugs extends MX_Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->load->model('plugs');
    }

    public function index()
    {
        $this->load->view('plugs/index');
    }

    public function get_plugin_meta()
    {
        echo $this->plugs->mymeta(); // this is the method from Plugs Models
    }   

}

But when I tried to access the URL http://localhost/mysite/admin/plugs/get_plugin_meta or http://localhost/mysite/admin/plugs it is giving me below error.

An Error Was Encountered

Unable to locate the model you have specified: plugs

So than how to load model in controller?

Your module is not plugs . It's admin because of modules/admin

You should use like $this->load->model('module/model');

Try this one:

$this->load->model('admin/plugs/plugs'); // module/folder/file

Important

Controller class name and model class name was conflicting.

Just rename model filename with: plugs_model.php

and change class name with: class Plugs_model extends CI_Model

$this->load->model('admin/plugs/plugs_model'); 
echo $this->plugs_model->mymeta(); // plugs model loded

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM