Petar
Petar

Reputation: 105

CodeIgniter: loading multiple models in the same controller

I searched the whole Internet and either there is no one mentioning my problem, or I'm stupid, or maybe it's just a bad day for coding.

What's the situation:

The "login" model is loaded from autoload.php, then in each controller's constructor I have $this->login->check(), which is checking if the user is logged in (obviously). Then in some of the methods I'm using the "source" model to connect to the database.

I tried loading both of the models from the autoload array, I also tried to load them in the way described here, but it's obviously for an old CI version (the thread is from 2008) and I tried all the possible ways I had in my mind.

Anyway, the result is this:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Source::$login

Filename: controllers/source.php

Line Number: 10

Fatal error: Call to a member function check() on a non-object in ...\application\controllers\source.php on line 10

Any ideas what I'm missing or how to fix it...? I'm stuck for hours and I don't have any ideas what I could do...

Edit 1: here is the code from the "source" controller:

class Source extends CI_Controller {

  function __construct() {
      parent::__construct();

      $this->load->model('login');

      $this->login->check();
  }

  function index() {
      // Pagination config, getting records from DB

      $this->load->view('templates/layout', $data);
  }

  function add() {
      $this->load->model('source', '', true);

      $btn = $this->input->post('btn');

      if(isset($btn)) {
          // More form validation

          if($this->form_validation->run() == TRUE) {
              if($btn == "Add") {
                  // here I am supposed to use the source model...
              }
          }
      }

      $data['page'] = 'source_add';

      $this->load->view('templates/layout', $data);
   }

 }

?>

Edit 2: login.php:

<?php

class Login extends CI_Model {

  function __construct() {
      parent::__construct();
  }

  function authenticate($username, $password) {
      // the login script comes here
  }

  function logged() {
      if($this->session->userdata('logged') == true) {
          return true;
      } else return false;
  }

  function check() {
      if(!$this->logged()) {
          redirect('/authentication');
      }
    }
  }
?>

Upvotes: 4

Views: 8783

Answers (2)

Chris Lloyd Sherwood
Chris Lloyd Sherwood

Reputation: 29

I resolved this issue by utilizing the hooks and turned the login process into a controller, thereby being able to access user information and setting access levels.

First I added the following to the hooks.php file in the config folder $hook['post_controller_constructor'][] = array('function' => 'check_login','filename' => 'authority.php','filepath' => 'hooks');

Then I have the following functions in a hook file called authority.php

[EDIT]Having reviewed this I am going to change it to a pre_controller_constructor and see if I can remove what seems to be a double page flash on initial construct.[/EDIT]

 function check_login(){
    $CI =& get_instance();

    $is_logged_in = $CI->session->userdata('is_logged_in'); 
    if(!$is_logged_in){
        $unauth_pages = array(your unauthorized pages go here);
        if(!in_array($CI->router->class,$unauth_pages)){
            $CI->session->set_userdata('before_login_url',current_url());
            redirect('login');          
        }
    }
}

function check_authority(){
    $CI =& get_instance();

    if($CI->session->userdata('usergroupID') == 'SUPADMIN'){return;}

    $page = $CI->router->class ;
    $method = $CI->router->method;
    $method = ($method=='index')?'':$method;
    $unauth_pages = array(your unauthorized pages go here); 
    if(in_array($page,$unauth_pages))return;

    $user_group = $CI->session->userdata('usergroupID');
    $CI->load->model('user_model');

    if($user_group == 'ADMIN' || $user_group == 'USER'){
        if($CI->session->userdata('timezone') == ''){
            date_default_timezone_set('Canada/Pacific');    
        } else {
            date_default_timezone_set($CI->session->userdata('timezone'));
        }
    }

    if( !$CI->user_model->authorized_content($CI->session->userdata('usergroupID'),$page, $method)){        
        redirect('unauthorized');
    }   

}

With the above I dont have to worry about checking on each page but instead utilize the ci framework to do the checking for me.. if its not in the unauth page array then it is a page that requires authorization checking.

Hope this works for you.

Upvotes: 0

Carlos Mora
Carlos Mora

Reputation: 1184

Conventionally, the classname of Models should end with _model, so it not collides with controllers with the same name, so try changing

class Login extends CI_Model {

to

class Login_model extends CI_Model {

Upvotes: 6

Related Questions