0

So let's say i have a controller named pages.

with this function.

function __construct(){
parent::__construct();
$this->auth['result'] = $this->is_logged_in();
}

The above code, calls for the extended function 'MY_Controller' which has a function called is_logged_in() and will simply fetch session data.

My first question would be, am i doing this authentication correct? or am i simply under-using it?

Anyways, my main question is regarding loading views.

Say i have many views, some views are accessed only by TYPE A user that has an access level different to that of TYPE B user.

Say both accounts are created differently, like in some online websites where there are users and shopowners that registered to advertise their shop irl, or for example, online travel. One account is user(normal/travelers) and one account is hotelowner(owns a hotel irl). So both will have overlapping views and some dedicated views for them.

How do i go about it when loading views?

Say in my controller i got functions,

public function view_home(){

}

public function view_userprofile(){

}

public function vieW_hotelprofile(){

}

You get my idea from above, i have separate functions for loading different views. But my question is that, how do i go about making it into one big function?

like for example,

public function view($page ='home' ){

}

the view function would be like a terminal for different accounts to access the views.

This would be simpler if i don't have to check access_levels. My first idea is to make a very long IF(){}ELSEIF(){} statement for example.

if($this->auth['result'] = 1 ){
load view here.
}

is this acceptable? also, how would i go about it efficiently? and regarding the authentication, this part i'm really confused if i'm using it the right way or not.

Also if i ever do a very long if statement, what values would i check? like i know you have to check the access level for certain views but do i also check the username logged in and match it to the database if it's accesslevel is the same?

JM Canson
  • 91
  • 5

1 Answers1

0

You can try using switch case

    public function loadView()
    {
            switch($this->auth['result']){
            case 1:
                $view= "home";
                break;
            case 2:
                $view= "userprofile";
                break;
            case 3:
                $view= "hotelprofile";
                break;          
            default:
                $view= "Welcome";
                break;

        }       
     $this->load->view($view);
    }
Nishant Nair
  • 1,999
  • 1
  • 13
  • 18
  • What's the difference between that and the if statement? Is the switch better suited to when there are many conditions and therefore many corresponding results? – JM Canson Feb 01 '17 at 05:17
  • You may have look at [ http://stackoverflow.com/questions/680656/what-is-the-difference-between-if-else-and-switch ] – Nishant Nair Feb 01 '17 at 05:23