douqin0676 2013-05-23 17:47
浏览 27
已采纳

CodeIgniter重用/扩展控制器

I have my controller

class Page extends CI_Controller {

    public function id() {
        $this->load->model('content');

        $page = $this->uri->segment(3, 0);
        if($page == 0)
            $page = $this->content->get_default_page($page);

        $data['navigation'] = $this->content->getNav();
        $data['pagename'] = $this->content->get_pagename($page);
        $data['content'] = $this->content->get_content($page);
        $this->load->view('main', $data);
    }
}

Now I'll try to explain. Im getting navigation, and navigation text from mysql (id, navName, navText). then im returning those elements in views/main_view.php in url like: http://abc.com/page/id/1 etc...

Now i need to create other controller like mySuperDuperModule which have some functions not just text.

The problem is that if i create new controller like Gallery(), i need to copy all the stuff from Page() controller to make website show the same.

Is there any way not to do that ?

  • 写回答

2条回答 默认 最新

  • dse55384 2013-05-23 18:46
    关注

    You can create base controller under /application/core/MY_Controller.php Here MY_ is the value specified for $config['subclass_prefix'] in /application/config/config.php

    class MY_Controller extends CI_Controller
    {
        protected $data;
        public function __construct()
        {
            parent::__construct();
            $this->data = array();
        }
        public function loadPage($page)
        {
            $this->load->model('content');
    
            if($page == 0)
                $page = $this->content->get_default_page();  // I hope this function will give default page from database table. (A Change from your code)
    
            $this->data['navigation'] = $this->content->getNav();
            $this->data['pagename'] = $this->content->get_pagename($page);
            $this->data['content'] = $this->content->get_content($page);
        }
    }
    

    Your modified Page Class in /application/controllers/page.php

    class Page extends MY_Controller
    {
        public function __construct()
        {
            parent::__construct();
        }
        public function id()
        {
            $page = $this->uri->segment(3, 0);
            parent::loadPage($page);
            $this->load->view('main', $this->data);
        }
    }
    

    and your new gallery controller can be in /application/controllers/gallery.php

    class Gallery extends MY_Controller
    {
        public function __construct()
        {
            parent::__construct();
        }
        public function id()
        {
            $page = 10;  // Assuming gallery page index is 10 in database table OR you can change any logic here.
            parent::loadPage($page);
            $this->load->view('main', $this->data);
        }
    }
    

    you can create as many controllers as you want by extending MY_Controller.

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

报告相同问题?

悬赏问题

  • ¥15 quartz框架,No record found for selection of Trigger with key
  • ¥15 锅炉建模+优化算法,遗传算法优化锅炉燃烧模型,ls-svm会搞,后面的智能算法不会
  • ¥20 MATLAB多目标优化问题求解
  • ¥15 windows2003服务器按你VPN教程设置后,本地win10如何连接?
  • ¥15 求一阶微分方程的幂级数
  • ¥15 关于#线性回归#的问题:【统计】回归系数要转化为相关系数才能进行Fisher' Z转化吗(相关搜索:回归模型)
  • ¥100 使用matlab解决含分段变量的优化问题
  • ¥15 matlab基于鲸鱼算法优化vmd
  • ¥20 PCB设计与制作问答
  • ¥20 课堂跑如何下载ppt