duanlujiaji10335 2016-10-17 10:12
浏览 15
已采纳

如何在视图中检索数据独立于codeigniter中的控制器

I have a sidebar in my site that receive some information from db and I can't use controller for retrieve data because I have different controller and same sidebar. How can I print this data in view page. when I wrote in P.h.P code in the view it shows an error that it cant define variables.

How could I do this?

  • 写回答

3条回答 默认 最新

  • dta25920 2016-10-17 13:33
    关注

    When you find that you need the same code in many different controllers a "custom library" (class) is the perfect choice. Documentation for creating your own libraries is found HERE.

    Controllers should be using models to get data from the database. Custom libraries can also use models just like controllers. Here is a very basic custom library called Sidebar. It depends on a model (sidebar_model) that will not be shown. The purpose of the Sidebar library is to return the variables need by the sidebar_view file.

    File: application/libraries/Sidebar.php

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    class Sidebar
    {
        protected $CI; // Read the documentation link to see why this is needed.
    
        public function __construct()
        {
            $this->CI = & get_instance();
            $this->CI->load->database();  //only needed if not already done 
            $this->CI->load->model('sidebar_model');
        }
    
        public function get_sidebar_data()
        {
            return $this->CI->sidebar_model->get_sidebar();
        }
    
    }
    

    The library method get_sidebar_data() returns the variables for the view.

    Here is a controller that uses the custom library. It will use the custom library and a view file (not shown) containing HTML for the sidebar.

    File: application/controllers/Main.php

    class Main extends CI_Controller
    {
        function __construct()
        {
            parent::__construct();
            $this->load->library('sidebar'); //can also be autoloaded
        }
    
    
        public function index()
        {
            $data['sidebar'] = $this->sidebar->get_sidebar();
            $this->load
                 ->view('banner')
                 ->view('sidebar_view', $data)
                 ->view('main_view')
                 ->view('footer_view');
        }
    
    }
    

    Any other controller that needs to show the sidebar would use this same pattern.

    This controller loads four different view files and is using "method chaining" which is encouraged. Method chaining executes a tiny bit faster. But the best reason for using it? Less typing.

    The method chaning could also be type like this:

    $this->load->view('banner')->view('sidebar_view', $data)->view('main_view')->view('footer_view');
    

    But, IMO, putting each ->view() on a separate line makes it easier to read.

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

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备