Welcome Guest, Not a member yet? Register   Sign In
One if statement to rule them all
#1

[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?
#2

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

[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?
#4

[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
}
#5

[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;
        }
    }
    
}
#6

[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
#7

[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');
    }
    
}




Theme © iAndrew 2016 - Forum software by © MyBB