CodeIgniter: loading multiple models in one controller

I searched the entire Internet, and either no one mentioned my problem, or I'm dumb, or maybe it's just a bad day for coding.

What situation:

  • source controller
  • source model
  • login model

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

I tried to load both models from the startup array, I also tried to load them in the manner described here , but this is obviously for the old version of CI (stream from 2008), and I tried all the possible paths that I had in mind.

In any case, the result is as follows:

PHP error detected

Severity: notice

Message: Undefined property: Source :: $ login

Filename: controllers / source.php

Line Number: 10

Fatal error: call of the member function check () for the object in ... \ application \ controllers \ source.php on line 10

Any idea what I'm missing or how to fix it ...? I’m stuck for hours and I have no idea 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'); } } } ?> 
+4
source share
2 answers

Typically, the model class name should end in _model, so it does not interfere with controllers with the same name, so try changing

 class Login extends CI_Model { 

to

 class Login_model extends CI_Model { 
+6
source

I solved this problem using hooks and turned the login process into the controller, thereby having access to 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] After reviewing this, I'm going to change it to pre_controller_constructor and see if I can remove what seems like a double flash page on the initial construction. [/ 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 should not worry about checking on every page, but instead use the ci framework to check for me. If this is not in the unauth array, then this is a page that requires credentials.

Hope this works for you.

0
source

All Articles