(10-30-2016, 12:47 PM)cartalot Wrote: so preface this with these are opinions, different developers will do it different ways.
my suggestion would be to have separate controllers. you take a few minutes in the beginning to set them up, and then you never have to worry about it again. a variation on this would be that the super admin has access to a controller with the shared tools - and they alone have access to a controller with the 'super admin only' tools.
keep your controllers 'thin' - put the real work in the models, so then both controllers can call the common functions and you are not repeating code. however you are going to do your logged in validation - put that code in a model - and call it from the constructor of your controller or your my controller. instead of just confirming this is someone or a super admin that is logged in - return an object (or array) that you can then use for all your methods. for example if you return $this->superadmin in your constructor, then $this->superadmin will be available for all your controller methods, models and views. another bonus is then you don't have to mess with session code at all except for the initial check in. finally make a template so you aren't having to call headers and footers in your controllers.
Thanks for your response, cartalot.
Ok, I decided to use 2 controllers for the 2 admin areas: User.php (for normal users) and Admin.php (for the super-admin).
I reworked my original controller a bit, which is now called User.php. As Ion Auth automatically saves the logged in user as a session variable, I decided to instantiate a user object in the constructor, which I can then use in the other methods.
I am not sure about moving the login-verification into a separate model, as the Ion-Auth-class is already the model that handles all DB-queries etc. Will have to think about that...
PHP Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User extends MY_Controller
{
public $current_user;
function __construct()
{
parent::__construct();
// Load authentication library
$this->load->library('ion_auth');
// Check user status
if ( ! $this->ion_auth->logged_in())
{
redirect('auth/login');
}
// Check for admin status
if ($this->ion_auth->is_admin())
{
redirect('auth/login');
}
// Instantiate user object
$this->current_user = $this->ion_auth->user()->row();
}
public function index()
{
$data['userdetails'] = $this->current_user->first_name;
$this->load->view('common/header');
$this->load->view('common/top_nav');
$this->load->view('common/test', $data);
$this->load->view('common/footer');
}
}
Finally, I will have to think about a template-engine like you suggested, but that is a topic for another day I guess...