How to load model in CI 3 with HMVC?

4.1k views Asked by At

I'm using the framework CodeIgniter 3 with HMVC extension and I can't load model. When I try do it, I see next error: Unable to locate the model you have specified: Protection.

Model file (protection.php) placed at /application/modules/main/models/ and extends this code:

class Protection extends MX_Model
    { 
    function Protection()
    {
        parent::Model();
    }
    function check($val)
    {
        $tpl = "![^\w\d\s]*!";
        if(is_array($val))
        {
            foreach($val as $k => $v)
            {
                $val[$k] = preg_replace($tpl,"",$v);
            }
        }
        else
        {
                $val = preg_replace($tpl,"",$val);
        }
        return $val;
    }
}

Controller file placing at /application/modules/main/controllers/ with this code for load model and call her function:

$this->load->model('protection');
$gj = "I'--m_Y%ou#r-Fat/+her";
$this->prt->check($gj);
echo $gj;

I tried rename model file, place his in other folders, but all it was failed.

1

There are 1 answers

1
AudioBubble On BEST ANSWER

HMVC models should extend CI_Model only the controller use MX_ on controller And also for a model name instead of just having protection as model name I think would be best to have model_protection so codeigniter will not get confused.

modules > 

modules > main >

modules > main > controllers

modules > main > models

modules > main > views

On the constructor

Not use parent::Model();

But use parent::__construct(); Like in my example

On controller

$this->load->model('modulename/model_protection');

$this->model_protection->check($key);

File name Model_protection.php

<?php 

class Model_protection extends CI_Model {

public function __construct() {

parent::__construct();

}

public function check($val) {

}

}

Tip all controllers and models and libraries should have there first letter as uppercase on file name and classname