简体   繁体   中英

Why I am getting forbidden error during model call in controller

My controller, if I run this, it works properly

class Front_controller extends CI_Controller{ 
   public function index(){ 
      $this->load->view('front/index'); 
   } 
}

if I call model

class Front_controller extends CI_Controller{

   public function  index(){
      $data['list'] = $this->Mymodel->getdata(); 
      $this->load->view('front/index',$data); 
   } 
}

it gives :

403 forbidden error and 500 internal server error

You need to load the model before using it, like this:

$this->load->model('Mymodel');
$this->Mymodel->mymodelmethod();

More info here: https://codeigniter.com/user_guide/general/models.html#loading-a-model

Update your code:

    class Front_controller extends CI_Controller{
       public function  index(){
          $this->load->model('Mymodel');
          $data['list'] = $this->Mymodel->getdata(); 
         $this->load->view('front/index',$data); 
     }
    }

Before use of model in CI, you need to load that model by using the method $this->load->model('modelName');

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