开发者

code igniter :: includes?

开发者 https://www.devze.com 2023-01-25 06:55 出处:网络
I am new to code igniter and am still learning the best practices. In my site I have two menus, One for users who are logged in.

I am new to code igniter and am still learning the best practices.

In my site I have two menus,

One for users who are logged in.

On开发者_StackOverflow社区e for users who are logged out.

I have these in two files in the views folder.

My question is:

How should I go about including these?

my best guess is to

in the controller call

if($loggedin)
  $menu = $this->load->view('loggedin', true);
else
  $menu = $this->load->view('loggedout', true);
$this->load->view('main', array('menu' => $menu));

So basically pass the correct menu code into the view and echo that out.

Is there a better way of doing this?


you can break strict MVC and have something like this in your view:

<?php
// in your controller or when the user logs in:
$this->session->set_userdata('logged_in', 'true');
?>

your view:

<?php
  // Logged in?
  if ( ! $this->session->userdata('logged_in') {
      $this->load->view('loggedout');
  } else {
      $this->load->view('loggedin');
  }
?>

remember MVC is a concept, it isn't set in stone.


You can load as many views as you like in the function. So for example:

if($loggedin){
  $this->load->view('loggedin');
} else {
  $this->load->view('loggedout');
}

$this->load->view('main');

Makes it a little cleaner but probably negligible efficiency savings.


I would suggest you to use plugins.

put this in to plugin

if($loggedin)
  $menu = $this->load->view('loggedin', true);
else
  $menu = $this->load->view('loggedout', true);
0

精彩评论

暂无评论...
验证码 换一张
取 消