John
John

Reputation: 33

Codeigniter Redirect or Load view with templates

I don't know how to phrase this question on google so i couldn't find any answers.

In my Views folder, i have templates folder with header,navbar,footer inside.

Whenever i load a view from my controller i would have to do this,

$this->load->view('template/header');
$this->load->view('template/navbar');
$this->load->view('pages/pagename');
$this->load->view('template/footer');

How do i do this with redirect? I don't know why but whenever i see code snippets of successful logins or failures they always use the redirect function instead of load view like the above.

for example:

function __construct() {
    parent::__construct();
    if($this->ion_auth->logged_in()==FALSE)
    {
        redirect('pages/login');
    }
}

or can i use this and will this still be acceptable?

function __construct() {
    parent::__construct();
    if($this->ion_auth->logged_in()==FALSE)
    {
      $this->load->view('template/header');
      $this->load->view('template/navbar');
      $this->load->view('pages/login');
      $this->load->view('template/footer');
    }
}

Upvotes: 0

Views: 871

Answers (3)

ivanavitdev
ivanavitdev

Reputation: 2888

Rather than construct you can use remap. to redirect if the user is logged or not

REMAP

public function _remap($method, $params = array()){
   if(method_exists($this, $method)){
      if($this->ion_auth->logged_in()==FALSE){
         return call_user_func_array(array($this, $method), $params); //home page
      }
      return call_user_func_array(array($this, 'login'), $params); //if not logged in
  }
  show_404();
}

LOGIN

public function login() {
  $this->load->view('template/header');
  $this->load->view('template/navbar');
  $this->load->view('pages/login');
  $this->load->view('template/footer');
}

Upvotes: 0

geeth
geeth

Reputation: 714

function __construct() {
    parent::__construct();
    if($this->ion_auth->logged_in()==FALSE)
    {
        redirect('controller/login');
    }
}

in your controller, create a function called login

function login() {
  $this->load->view('template/header');
  $this->load->view('template/navbar');
  $this->load->view('pages/login');
  $this->load->view('template/footer');
}

Upvotes: 1

Yadhu Babu
Yadhu Babu

Reputation: 1521

In redirect you need to use controller/method_name

redirect('controllername'); 

or

redirect('controllername/method');

Upvotes: 0

Related Questions