Welcome Guest, Not a member yet? Register   Sign In
Base Controller Usage with Flexi Auth
#1

Hi All,

I have already been able to get Flexi Auth working on my codeigniter 3 application however I would like to increase code reuse within my application and reduce duplicate code.

As such I was hoping to move the following code contained within my controllers __construct function to a base controller that would be called by each subsequent controller requiring it through extends.

The existing code that works is:
Controllers except Login.php:

PHP Code:
function __construct() {
 
parent::__construct();
 
// IMPORTANT! This global must be defined BEFORE the flexi auth library is loaded!
 // It is used as a global that is accessible via both models and both libraries, without it, flexi auth will not work.
 
$this->auth = new stdClass;

 
// Load 'standard' flexi auth library by default.
 
$this->load->library('flexi_auth');

 
// Check user is logged in as an admin.
 // For security, admin users should always sign in via Password rather than 'Remember me'.
 
if (! $this->flexi_auth->is_logged_in_via_password() || ! $this->flexi_auth->is_admin())
 {
 
// Set a custom error message.
 
$this->flexi_auth->set_error_message('You must login as an admin to access this area.'TRUE);
 
$this->session->set_flashdata('message'$this->flexi_auth->get_messages());
 
redirect('login/index');
 }
 
// Define a global variable to store data that is then used by the end view page.
 
$this->data null

Login.php
PHP Code:
function __construct() {
 
parent::__construct();
 
// IMPORTANT! This global must be defined BEFORE the flexi auth library is loaded!
 // It is used as a global that is accessible via both models and both libraries, without it, flexi auth will not work.
 
$this->auth = new stdClass;

 
// Load 'standard' flexi auth library by default.
 
$this->load->library('flexi_auth');
 
// Redirect users logged in via password (However, not 'Remember me' users, as they may wish to login properly).
 
if ($this->flexi_auth->is_logged_in_via_password() && uri_string() != 'login/logout')
 {
 
// Preserve any flashdata messages so they are passed to the redirect page.
 
if ($this->session->flashdata('message')) { $this->session->keep_flashdata('message'); }

 
// Redirect logged in admins (For security, admin users should always sign in via Password rather than 'Remember me'.
 
if ($this->flexi_auth->is_admin())
 {
 
redirect('site/admin');
 }
 else
 {
 
redirect('site/public');
 }
 }

 
// Define a global variable to store data that is then used by the end view page.
 
$this->data null;


My New Base controller located in the application/core directory:

PHP Code:
class MY_Controller extends CI_Controller {
 function 
__contruct() {
 
parent::__construct();
 }
 function 
is_logged_in_user_admin() {
 if (!
$this->flexi_auth->is_logged_in_via_password() || !$this->flexi_auth->is_admin()) {
 
// Set a custom error message.
 
$this->flexi_auth->set_error_message('You must login as an admin to access this area.'TRUE);
 
$this->session->set_flashdata('message'$this->flexi_auth->get_messages());
 
redirect('login/index');
 }
 }

 function 
is_logged_in_user() {
 
// Redirect users logged in via password (However, not 'Remember me' users, as they may wish to login properly).
 
if ($this->flexi_auth->is_logged_in_via_password() && uri_string() != 'login/logout')
 {
 
// Preserve any flashdata messages so they are passed to the redirect page.
 
if ($this->session->flashdata('message')) { $this->session->keep_flashdata('message'); }

 
// Redirect logged in admins (For security, admin users should always sign in via Password rather than 'Remember me'.
 
if ($this->flexi_auth->is_admin())
 {
 
redirect('site/admin');
 }
 else
 {
 
redirect('site/public');
 }
 }
 }


New Base Controller being called by another controller that extends MY_Controller:

PHP Code:
function __construct() {

parent::__construct();
// IMPORTANT! This global must be defined BEFORE the flexi auth library is loaded!
// It is used as a global that is accessible via both models and both libraries, without it, flexi auth will not work.
$this->auth new stdClass;
$this->load->library('flexi_auth');
parent::is_logged_in_user_admin();
$this->data NULL;


Could someone please help me with pointing me in the direction of where I can read up about extending the CI_Controller in Codeigniter 3 and how I should be doing this sort of thing.

If anyone has any ideas on what I have done wrong with the above please let me know as I have been trying to look at this for sometime and am still stumped at what I might be missing.

Any help is greatly appreciated.
Reply
#2

I should add that the errors I am currently getting when trying to run the application are on the login/index page: errors have been truncated:

Code:
A PHP Error was encountered

Severity: Warning

Message: Creating default object from empty value

Filename: models/Flexi_auth_lite_model.php

Line Number: 46

Backtrace:

File: /home/itconstr/public_html/develop/application/models/Flexi_auth_lite_model.php
Line: 46
Function: _error_handler

File: /home/itconstr/public_html/develop/application/libraries/Flexi_auth_lite.php
Line: 43
Function: model

File: /home/itconstr/public_html/develop/application/libraries/Flexi_auth.php
Line: 38
Function: library

File: /home/itconstr/public_html/develop/application/controllers/Login.php
Line: 9
Function: library

File: /home/itconstr/public_html/develop/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Notice

Message: Undefined property: stdClass::$session_name

Filename: libraries/Flexi_auth_lite.php

Line Number: 191

Backtrace:

File: /home/itconstr/public_html/develop/application/libraries/Flexi_auth_lite.php
Line: 191
Function: _error_handler

File: /home/itconstr/public_html/develop/application/core/MY_Controller.php
Line: 24
Function: is_logged_in_via_password

File: /home/itconstr/public_html/develop/application/controllers/Login.php
Line: 12
Function: is_logged_in_user

File: /home/itconstr/public_html/develop/index.php
Line: 292
Function: require_once

and Message: Undefined property: stdClass::$session_data
Reply
#3

Try changing this:
Code:
parent::is_logged_in_user_admin();
to this:
Code:
$this->is_logged_in_user_admin();

Additionally, if you're going to need the auth property defined before loading the flexi_auth library, you might as well define it in your base controller. I would also move loading the library into the base controller, but you might want to put it into a method (possibly along with the definition of the auth property). Then you could call the method conditionally, depending on whether you are using authentication in the current controller.
Reply
#4

Hi MWhitney,

Thank you very much for your response it has fixed this issue!

The additional note is also very helpful and I will take a look at my code to see which approach is more beneficial at this stage.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB