CodeIgniter Forums
Constructor in controller is behaving strangely - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Constructor in controller is behaving strangely (/showthread.php?tid=52989)



Constructor in controller is behaving strangely - El Forum - 07-05-2012

[eluser]brianatlarge[/eluser]
In my controller, I'm checking if a session variable is set. If it is, then I know the user is logged in and I can display the site. If not, then I'll display the login page.

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $user_email = $this->session->userdata('user_email');
        if(!empty($user_email)) {
            $this->index();
        } else {
            $this->login();
        }
    }

    public function index()
    {
        $this->load->view('admin/home');
    }

    public function login()
    {
       $this->load->view('admin/login');
    }
}

The problem with this is it's loading both the home and the login views. What's the deal?



Constructor in controller is behaving strangely - El Forum - 07-05-2012

[eluser]nagata[/eluser]
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $user_email = $this->session->userdata('user_email');

        if(!empty($user_email)) {
        //logged in, do nothing, let the index load safely...
        } else {
           redirect("admin/login");
        }
    }

    public function index()
    {
        $this->load->view('admin/home');
    }

    public function login()
    {
       $this->load->view('admin/login');
    }
}
Its just Index is an initial page loaded if there is no function defined when u are going somewhere
ex: u have admin panel controller,
if u visit admin it will load by default the index()
if there was a function given it will then admin/(function)
...
u see...
it should do anything when user is logged in, if he isnt then just redirect him to login page...



Constructor in controller is behaving strangely - El Forum - 07-05-2012

[eluser]Aken[/eluser]
The constructor can do logic, but can't output other controller methods. Use it to check for your login, but not to display views (or load methods that display views).


Constructor in controller is behaving strangely - El Forum - 07-05-2012

[eluser]brianatlarge[/eluser]
[quote author="nagata" date="1341513549"]
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $user_email = $this->session->userdata('user_email');

        if(!empty($user_email)) {
        //logged in, do nothing, let the index load safely...
        } else {
           redirect("admin/login");
        }
    }

    public function index()
    {
        $this->load->view('admin/home');
    }

    public function login()
    {
       $this->load->view('admin/login');
    }
}
Its just Index is an initial page loaded if there is no function defined when u are going somewhere
ex: u have admin panel controller,
if u visit admin it will load by default the index()
if there was a function given it will then admin/(function)
...
u see...
it should do anything when user is logged in, if he isnt then just redirect him to login page...
[/quote]

Hmm... when I do this, it just loops the redirect part.


Constructor in controller is behaving strangely - El Forum - 07-05-2012

[eluser]nagata[/eluser]
Well its cause U should have an seperate controller responding for user authorization...
Like a members controller with reg and login...
and it should redirect to "members/login"...