duanchandun1860 2014-06-15 06:04
浏览 46
已采纳

在CodeIgniter站点中包含多个标头

I am working on a fairly large CodeIgniter project. There will be more pages than I can count. Right now, I have a few different headers, depending on what page the user is visiting and also depending on if the user is logged in. For example, if the user is not logged in (no session variable stored) then I want to display header1 which shows some basic stuff like "Signup, Login". If the user IS already logged in, I want to show different menu items like "logout". That's a very basic example. The changes can be pretty extensive, so it wouldn't make sense for me to have one header file with control flow logic in it.

I'm looking for a way to include the appropriate header without having to write $this->load->view('header') in every method I have in my controllers. It looks like another option is to write that line in every view that I have. To make this dynamic I created a MY_Controller file and included something like this in it:

public function get_header()
{
    if ($this->session->userdata('user_id')) {
        $this->load->view('headers/logged-header');
    } else {
        $this->load->view('headers/header-home');
    }

}

Then I include <?php MY_CONTROLLER::get_header(); ?> in places that I need it (i.e inside methods or views.)

Is there a way to "hook" this in somehow, where I can simply write this code once, and then the application process it every time it loads one of my methods that call up a page?

  • 写回答

2条回答 默认 最新

  • dslkchyv673627 2014-06-15 08:24
    关注

    This is a very typical need that could be easily handled by using the OOP, which CI is using in all of its controllers.
    Since all of your self-defined controllers must inherit from CI_Controller, you can just write a base controller(class) that inherits from CI_Controller, which do some very basic things like :

    • do some basic operation in its __construct that all of its subclass may need(e.g. auto login logic, and you can put get_header here)
    • provide methods that all of its subclass may need like right check, pagination widget generator...
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度