Codeigniter + Ion Auth part 2

Create the User controller

We will create a bare-bone controller named User.php inside application/controllers. As you will see, we will load the Ion Auth library inside the constructor in order to use its methods:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
 
class User extends MY_Controller {
     
    function __construct()
    {
        parent::__construct();
        $this->load->library('ion_auth');
    }
 
    public function index()
    {
        $this->load->view('welcome_message');
    }
 
    public function login()
    {
        echo 'Here we will make the login form';
    }
 
    public function logout()
    {
        echo 'here we will do the logout';
    }
}

We will also need to create a controller that will be accessible only to those users that are logged in. So let’s create a Dashboard.php file inside our application/controllers. This will extend a controller named Auth_Controller:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
  
class Dashboard extends Auth_Controller {
  
    public function index()
    {
        echo 'Hello from the dashboard';
    }
}

The Auth_Controller that we will create soon will be verifying if the users are logged in. If they aren’t, they will be redirected to the login() method of the User controller.

Creating the MY_Controller and the Auth_Controller

Let’s create a MY_Controller.php file inside application/core:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
  
    protected $data = array();
    function __construct()
    {
        parent::__construct();
        $this->data['page_title'] = 'CI App';
        $this->data['page_description'] = 'CI_App';
        $this->data['before_closing_head'] = '';
        $this->data['before_closing_body'] = '';
    }
  
    protected function render($the_view = NULL, $template = 'public_master')
    {
        if($template == 'json' || $this->input->is_ajax_request())
        {
            header('Content-Type: application/json');
            echo json_encode($this->data);
        }
        elseif(is_null($template))
        {
            $this->load->view($the_view,$this->data);
        }
        else
        {
            $this->data['the_view_content'] = (is_null($the_view)) ? '' : $this->load->view($the_view, $this->data, TRUE);
            $this->load->view('templates/' . $template . '_view', $this->data);
        }
    }
}

What we’ve done here is some basic templating for our pages. Also we’ve passed some default data that will be passed to the views like the page title and the page description. Now, if some controller extends the MY_Controller, he will pass the data to the views by using $this->data[‘variable_name’], and will render the page by calling $this->render(‘the_view’). Simple enough.

Now we will need to create the Auth_Controller. We will create the class in the same MY_Controller.php file, just after the definition of the MY_Controller class.

class Auth_Controller extends MY_Controller {
    function __construct() {
        parent::__construct();
        $this->load->library('ion_auth');
        if($this->ion_auth->logged_in()===FALSE)
        {
            redirect('user/login');
        }
    }
    protected function render($the_view = NULL, $template = 'auth_master')
    {
        parent::render($the_view, $template);
    }
}

As you can see, Auth_Controller will take over the data from the MY_Controller. Also, you may observe the fact that inside the constructor, the class verifies that the user is logged in by using the Ion Auth method logged_in() (more on this method can be found here: http://benedmunds.com/ion_auth/#logged_in). If the user is not logged in, he is redirected to the user/login page.

by kushan


avatar
  Subscribe  
Notify of