CodeIgniter Forums
One if statement to rule them all - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: One if statement to rule them all (/thread-22414.html)



One if statement to rule them all - El Forum - 09-08-2009

[eluser]dennismonsewicz[/eluser]
Is there anyway to write the following without having to write it in each function?

Code:
function main() {
        if($this->session->userdata('logged_in') == TRUE) {
            $data['username'] = $this->session->userdata('email');
            $data['firstN'] = $this->session->userdata('first_name');
            $data['lastN'] = $this->session->userdata('last_name');
            $data['city'] = $this->session->userdata('city');
            $data['state'] = $this->session->userdata('state');
            $this->load->view('mrmain/includes/header_view');
            $this->load->view('admin/main_view', $data);
            $this->load->view('mrmain/includes/footer_view');    
        } else {
            redirect('/');
        }    
    }

I am interested in not have to write the if 'logged_in' == TRUE everytime I write a new function in the admin section... how would I write this once in my parent function?


One if statement to rule them all - El Forum - 09-08-2009

[eluser]brianw1975[/eluser]
search for MY_Controller or extending Controller


One if statement to rule them all - El Forum - 09-08-2009

[eluser]dennismonsewicz[/eluser]
ok... but what would my if else look like? if logged_in == true do what? the else would just redirect them home but what would the if statement do if evaluated true?


One if statement to rule them all - El Forum - 09-08-2009

[eluser]brianw1975[/eluser]
simply reverse your logic...

Code:
if($this->session->userdata('logged_in') != TRUE)
            redirect('/');
else{
            $data['username'] = $this->session->userdata('email');
            $data['firstN'] = $this->session->userdata('first_name');
            $data['lastN'] = $this->session->userdata('last_name');
            $data['city'] = $this->session->userdata('city');
            $data['state'] = $this->session->userdata('state');
   // continue on and let the controller do what it needs to do
}



One if statement to rule them all - El Forum - 09-08-2009

[eluser]bretticus[/eluser]
You could still extend Controller, but it's also as easy as:
Code:
class something extends Controller {
    function something() {
        parent::Controller();
    }

    function main() {
        $this->_protect();
        $data['username'] = $this->session->userdata('email');
        $data['firstN'] = $this->session->userdata('first_name');
        $data['lastN'] = $this->session->userdata('last_name');
        $data['city'] = $this->session->userdata('city');
        $data['state'] = $this->session->userdata('state');
        $this->load->view('mrmain/includes/header_view');
        $this->load->view('admin/main_view', $data);
        $this->load->view('mrmain/includes/footer_view');
            //// moving on...
    }

    function _protect() {
        if($this->session->userdata('logged_in') === FALSE) {
            redirect('/');
            exit;
        }
    }
    
}



One if statement to rule them all - El Forum - 09-09-2009

[eluser]ggoforth[/eluser]
You could also do it with hooks. I am currently using a hook to check the status of a session var, and redirecting to login if not logged in. Works well. I do like (and probably would have used) MY_controller if I had it to do over again, as it's a bit more flexible, but it's working now, so it's all good.

Greg


One if statement to rule them all - El Forum - 09-09-2009

[eluser]Phil Sturgeon[/eluser]
+1 for MY_Controller. On PyroCMS I use several types of controller classes. MY_Controller adds logic to ALL controllers then I have Public_Controller and Admin_Controller to add shared logic to the front and back ends respectively.

If you just want to do this on a per-controller basis or don't want the extra complication, just do this:

Code:
class something extends Controller {
    function something() {
        parent::Controller();

        if($this->session->userdata('logged_in') === FALSE)
        {
            redirect('/');
        }
    }

    function main()
    {
        $data['username'] = $this->session->userdata('email');
        $data['firstN'] = $this->session->userdata('first_name');
        $data['lastN'] = $this->session->userdata('last_name');
        $data['city'] = $this->session->userdata('city');
        $data['state'] = $this->session->userdata('state');
        $this->load->view('mrmain/includes/header_view');
        $this->load->view('admin/main_view', $data);
        $this->load->view('mrmain/includes/footer_view');
    }
    
}