doufu9947 2011-08-24 13:47
浏览 20
已采纳

库标题视图中的库调用 - CodeIgniter

I need to call some library functions in order to correctly display some data in my header view - what is the best/correct way to approach this?

I take it I'm supposed to call this in all my controllers but it seems a bit redundant. Should I take this route? What sort of non 'hacky' method do you suggest?

Edit:

I have a view which outputs the header portion of the page. In this view I have a menu which varies depending whether you're logged in, have any favorites etc. To determine what to display in the menu, I must refer to some libraries, for example, an authentication and favorites library.

I tried calling the library from the view, but it gives me an error.

I suppose I could load the controller and pass the data to the view in every controller but I would have a lot of repetitive code. Is this the way to go?

  • 写回答

3条回答 默认 最新

  • douju6752 2011-08-24 14:26
    关注

    Yes, manually assigning the same data to a partial view in every controller or method is redundant. You want to avoid this.

    Any decent template library should be able to handle this for you. You can google up a solution or write your own.

    Here, in mockup code, is what it may resemble:

    class Template {
    
        public $data = array();
    
        function area($area_name)
        {
            $CI =& get_instance();
            $data = isset($this->data[$area_name]) : $this->data[$area_name] ? NULL;
            $CI->load->view('templates/'.$area_name, $data);
        }
    
        function set_data($area_name, $data)
        {
            $this->data[$area_name] = $data;
        }
    }
    

    Then in the controller, something like this:

    $this->template->set_data('header', $my_data);
    

    Then in the view, something like this:

    <header><?php echo $this->template->area('header'); ?></header>
    <div><?php echo $this->template->area('content'); ?></div>
    <footer><?php echo $this->template->area('footer'); ?></footer>
    

    This is over-simplified, and there are a million ways to handle it, but I definitely suggest writing some kind of class to handle your templates rather than just using $this->load->view() for everything, even if it is just a wrapper for loading views.

    To avoid manually setting that same data in every controller, use a MY_Controller, set it in the template class __construct(), or call it directly from the view file from whatever the source is (model, library, etc.). Father MVC may shed a tear, but sometimes this is easiest or even makes the most sense.

    Quick example of how to use a controller extension:

    // File: application/core/MY_Controller.php
    class MY_Controller extends CI_Controller {
    
        public function __construct()
        {
            parent::__construct();
            $data['var_name'] = $this->some_lib->get_data();
            $data['var_name2'] = $this->some_model->get_more_data();
            $this->template->set_data('header', $data);
        }
    }
    

    Then, all your controllers would extend MY_Controller rather than CI_Controller and the header data would already be loaded. This is just a simplified glimpse into another topic altogether, much much more is possible with this method.

    I tried calling the library from the view, but it gives me an error.

    This shouldn't happen if the library is loaded and you are calling it correctly. The syntax is $this->library_name->method().

    In any case, I definitely recommend writing or borrowing some kind of Template class.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码