dth981485742 2014-01-14 09:42
浏览 511
已采纳

如何在codeigniter中调用另一个控制器中的一个控制器功能

I have one controller named home.php in which a function named podetails is there. I want to call this function in another controller user.php.
Is it possible to do so? I have read about HMVC in CI, but I want to know is it possible to do without using hmvc?

  • 写回答

2条回答 默认 最新

  • dqrmkdu25623 2014-01-14 11:16
    关注

    To extend controller please either follow this tutorial or see some code below.


    differences between private/public/protected


    make a file in folder /application/core/ named MY_Controller.php

    Within that file have some code like

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class MY_Controller extends CI_Controller {
    
        protected $data = Array(); //protected variables goes here its declaration
    
        function __construct() {
    
            parent::__construct();
            $this->output->enable_profiler(FALSE); // I keep this here so I dont have to manualy edit each controller to see profiler or not        
            $this->load->model('some_model'); //this can be also done in autoload...
            //load helpers and everything here like form_helper etc
        }
    
        protected function protectedOne() {
    
        }
    
        public function publicOne() {
    
        }
    
        private function _privateOne() {
    
        }
    
        protected function render($view_file) {
    
            $this->load->view('header_view');
            if ($this->_is_admin()) $this->load->view('admin_menu_view');
    
            $this->load->view($view_file . '_view', $this->data); //note all my view files are named <name>_view.php
            $this->load->view('footer_view');
    
        }
    
        private function _isAdmin() {
    
            return TRUE;
    
        }
    
    }
    

    and now in any of yours existing controllers just edit 1st or 2nd line where

    class <controller_name> extends MY_Controller {
    

    and you are done

    also note that all your variables that are meant to be used in view are in this variable (array) $this->data

    example of some controller that is extended by MY_Controller

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class About extends MY_Controller {
    
        public function __construct() {
    
            parent::__construct();
    
        }
    
        public function index() {
            $this->data['today'] = date('Y-m-d'); //in view it will be $today;
            $this->render('page/about_us'); //calling common function declared in MY_Controller
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?