Authentication, CodeIgniter templates
If the user is logged in
I want to show a different menu then if the user is not logged in.
Is the best way to accomplish this to make just two different views, and including them depending if the user is logged in or not (check in controller)
class Page extends CI_Controller {
protected $file = 'index';
public function index()
{
if ($this->auth->logged_in()) {
$this->file = 'logged_in';
}
$data['title'] = 'Hem';
$this->load->view('templates/header', $data);
$this->load->view('templates/menu/' . $this->file . '');
$this->load->view('home');
$this->load->view('templates/sidebar/' . $this->file . '');
$this->load->view('templates/footer');
}
}
That is my solution so far, how 开发者_如何学运维can Improve it?
If it's something as small as a header or a menu, I usually just break it into view partials and determine which one to display in the view. I also call all of the other view partials inside my main template view so it only requires a single $this->load->view()
. That approach would give you this in your controller:
class Page extends CI_Controller {
public function index()
{
$view_data['main_content'] = 'home';
$this->load->view('templates/default');
}
}
and this in your view:
$this->load->view('templates/partials/header');
if ($this->auth->logged_in())
{
$this->load->view('templates/partials/menu');
}
else
{
$this->load->view('templates/partials/index');
}
$this->load->view($main_content);
$this->load->view('templates/partials/sidebar');
$this->load->view('templates/partials/footer');
This way you are always calling the same template and you can just set the $main_content
which is the actual view you want to load and everything else that stays the same from page-to-page is already there.
精彩评论