0

I'm just trying out the Ion Auth library and it seems real good.

I have this main controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller {

        //Main page
    public function index() {
                $this->view_data['header'] = $this->load->view('main/header.php', '', true);
                $this->view_data['footer'] = $this->load->view('main/footer.php', '', true);
        $this->load->view('main/index', $this->view_data);                
    }
}

In the main view I have a link to /login

And the login controller looks like this

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once(APPPATH.'controllers/secure.php');
class Login extends Secure {

        //Login page
    public function index() {
                $this->load->library('ion_auth');
                $this->view_data['header'] = $this->load->view('main/header.php', '', true);
                $this->view_data['footer'] = $this->load->view('main/footer.php', '', true);
                $this->load->view('login/index', $this->view_data);                
    }
}

The Login-controller extends the Secure - controller where I have the actuall mechanism for checking if a user is loggedin or not.

Secure-controller:

<?php defined('BASEPATH') OR exit('No direct script access allowed');

//If any class extends with this class, do some stuff to show loginform etc
class Secure extends CI_Controller {
  function __construct() {
        parent::__construct();
        $this->load->library('ion_auth');

        //
        // Require members to be logged in. If not logged in, redirect to the Ion Auth login page.
        //
        if( ! $this->ion_auth->logged_in()) {
          redirect(base_url() . 'auth/login');
        }
    }
}

It works fine and redirects to the actual loginpage for user correctly.

BUT I don't want it to be redirected, but to embedded as a part of main/index

Instead of redirecting I would the template for auth login to be returned, something like:

return $this->load->view('auth/login', {data used for loginform}, true);

Is there any way to achieve this? Or do I have to create specific function in the secure controller and based on what those function returns I will show different templates?

I've tried this: *Secure-controller*

<?php defined('BASEPATH') OR exit('No direct script access allowed');

//If any class extends with this class, do some stuff to show loginform etc
class Secure extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('ion_auth');
    }

    public function isLoggedIn() {
        if( ! $this->ion_auth->logged_in()) {                        
                $this->view_data['identity'] = '';
                $this->view_data['message'] = '';
                $this->view_data['password'] = '';
                return $this->load->view('auth/login', $this->view_data, true);   
        }
        else {
            return '';
        }

    }
}

Login-controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once(APPPATH.'controllers/secure.php');
class Login extends Secure {

        //Login page
    public function index() {
                $this->load->library('ion_auth');
                $this->view_data['header'] = $this->load->view('main/header.php', '', true);
                $this->view_data['footer'] = $this->load->view('main/footer.php', '', true);

                $this->view_data['loginform'] = $this->isLoggedIn();

                $this->load->view('main/index', $this->view_data);                
    }
}

BUT then I just see the input fields and checkboxes, no text on the labels...

Can anyone give some pointer in what direction I should chose for implementing this? Please ask if I'm to unclear about my issue.

bestprogrammerintheworld
  • 5,417
  • 7
  • 43
  • 72

1 Answers1

1

You can load views inside of other views. Here's another answer that should help:

Best method of including views within views in CodeIgniter

Community
  • 1
  • 1
Ben Edmunds
  • 888
  • 4
  • 6