认证,CodeIgniter模板 [英] Authentication, CodeIgniter templates

查看:137
本文介绍了认证,CodeIgniter模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户已登录

我想显示不同的菜单,如果用户未登录。

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?

推荐答案

如果它是一个像标题或菜单一样小,我通常只是把它分成视图partials,在视图中显示。我也调用所有其他视图partials在我的主模板视图内,所以它只需要一个 $ this-> load-> view()。这种方法会给你在你的控制器中:

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

在您的视图中:

$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');

这样你总是调用相同的模板,你可以设置 $ main_content 这是您要加载的实际视图,以及其他在页面到页面之间保持不变的视图已经存在。

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.

这篇关于认证,CodeIgniter模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆