Codeigniter load model in controller constructor


Codeigniter load model in controller constructor : You can load model in codeigniter constructor instead of loading in each function. It is good practice to load the frequently used model in controller constructor. Here in this article we are going to explain how you can load model in controller constructor.


Syntax : Codeigniter load model in controller constructor

 function __contruct()
    {
        parent::__construct();
        $this->load->model('users');
    }

Example :

Here is complete example of loading model in constructor and calling its’s function in controller function.



<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Users extends CI_Controller
{
    function __contruct()
    {
        parent::__construct();
        $this->load->model('users');
    }

    function seeAllUser()
    {
        $data['users'] = $this->users->getAllUsers();        
        $this->load->view('my_view', $data);
    }
}

Advertisements

Add Comment

📖 Read More