Codeigniter整理我的控制器

时间:2013-07-04 10:17:51

标签: codeigniter controllers performance

我直接从手册中遵循了Codeigniter的配置 - 只是想知道是否有更简单或更有效的编码控制器的方式......

例如

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

3 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分: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);
    }
}

当然,可以通过其他方式管理视图加载,使视图加载所有公共部分。

答案 2 :(得分: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);
    }
}