2013-07-04 64 views
1

我跟着笨的配置直出手冊 - 如果有我的編碼控制器的簡單或更有效的方式很奇怪......笨整理我的控制器

如。

class Home extends CI_Controller { 

     public function index() 
     { 
      $this->load->helper('segment1'); 
      $this->load->model('segment1/Leftsidebar_model'); 
      $data['articles'] = $this->Leftsidebar_model->articles(); 
      $this->load->model('segment1/Default_model'); 
      $data['head'] = $this->Default_model->segment1(); 
      $data['segment1'] = $this->Default_model->segment1(); 
      $data['segment2'] = $this->Default_model->segment2(); 
      $this->load->model('Rightsidebar_model'); 
      $data['coming_up'] = $this->Rightsidebar_model->coming_up(); 
      $data['featured_pages'] = $this->Rightsidebar_model->featured_pages(); 
      $data['recommended_link'] = $this->Rightsidebar_model->recommended_link(); 
      $data['testimonials'] = $this->Rightsidebar_model->testimonials(); 
      $this->load->view('head_view', $data); 
      $this->load->view('header_view', $data); 
      $this->load->view('segment1/__leftSidebar_view', $data); 
      $this->load->view('segment1/__mainContent/default_view', $data); 
      $this->load->view('segment1/__mainContent/segment2_view', $data); 
      $this->load->view('__rightSidebar_view', $data); 
      $this->load->view('footer_view', $data); 
     } 
    } 

回答

0

助手和模型可以自動加載,這可以在confiq文件中指定。這將使您不必手動加載它們。

至於其餘:

您可以繼承CI_Controller。例如: 我的控制器擴展CI控制器,然後這將包含2種方法

1)加載頁頭和頭 2)加載頁面底部

然後你可以從My_Controller繼承您的控制器,並呼籲那些方法

load_page_top(); 
//insert whatever you have to load 
load_page_bottom(); 

除此之外剩下的就是你

如:

class Home extends MY_Controller 
{ 
    index() 
    { 
     $data = get_data(); 
     load_page_top(); 
     //insert your views here specific to the controller 
     load_page_bottom(); 
    } 

    get_data() 
    { 
     //gather all your needed data here and return it as an array 
     return data; 
    } 
} 
0

我喜歡這種結構,整潔乾淨。

class Home extends CI_Controller { 

    public function index() 
    { 
     // Load libraries, helpers, models 
     $this->load->helper('segment1'); 
     $this->load->model('segment1/Leftsidebar_model'); 
     $this->load->model('segment1/Default_model'); 
     $this->load->model('Rightsidebar_model'); 

     // Data for views 
     $data = array(
      'articles' => $this->Leftsidebar_model->articles(), 
      'head' => $this->Default_model->segment1(), 
      'segment1' => $this->Default_model->segment1(), 
      'segment2' => $this->Default_model->segment2(), 

      'coming_up' => $this->Rightsidebar_model->coming_up(), 
      'featured_pages' => $this->Rightsidebar_model->featured_pages(), 
      'recommended_link' => $this->Rightsidebar_model->recommended_link(), 
      'testimonials' => $this->Rightsidebar_model->testimonials() 
     ); 

     // Load views 
     $this->load->view('head_view', $data); 
     $this->load->view('header_view', $data); 
     $this->load->view('segment1/__leftSidebar_view', $data); 
     $this->load->view('segment1/__mainContent/default_view', $data); 
     $this->load->view('segment1/__mainContent/segment2_view', $data); 
     $this->load->view('__rightSidebar_view', $data); 
     $this->load->view('footer_view', $data); 
    } 
} 

當然,視圖加載可以通過其他方式進行管理,具有加載所有常見部分的視圖。

0

我只寫了一些額外的庫來渲染相似的頁面塊。 事情是這樣的:

class Display_Lib{ 
     private $_CI; 
     private $_template_data; 

     public function __construct() 
     { 
      $this->_CI =& get_instance(); 
     } 

     public function set($key, $value) 
     { 
      $this->_template_data[$key] = $value; 
     } 

     public function get($key) 
     { 
      return $this->_template_data[$key]; 
     } 

     public function get_template_data() 
     { 
      return $this->_template_data; 
     } 

     public function display_page($view, $data = array()) 
     { 
      $this->set('content', $this->_CI->load->view($view, $data, TRUE)); 
      $this->_CI->load->view('templates/main_template', $this->get_template_data()); 
     } 
} 

設置這個庫中自動加載:

$autoload['libraries'] = array('session', 'database', 'display_lib'); 

,並調用它的控制器:

class Main extends CI_Controller{ 
    public function index() 
    { 
     $some_data = array(); 
     $this->display_lib->display_page('views/main_view', $some_data); 
    } 
}