在y控制器函数codeigniter中调用x控制器函数

时间:2014-05-16 06:31:53

标签: php codeigniter controller

我是codeigniter的新手,我有两个控制器:utility.phpwelcome.php

utility.php中,我有函数:

   function getdata() {
     //code here
   }

   function logdata() {
     //code here
   }

welcome.php内,我有这个功能:

   function showpage() {
     //some code here
     //call functions here
   }

我想要做的是在welcome.php内,我想调用utility.php中的函数。我该怎么做呢?感谢。

3 个答案:

答案 0 :(得分:1)

来自here

的参考

要扩展控制器,请按照此tutorial或查看下面的代码。


private/public/protected

之间的差异

在名为/application/core/

的文件夹MY_Controller.php中创建一个文件

在该文件中有一些代码,如

<?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;

    }

}

现在你现有的任何控制器只需编辑第一行或第二行

class <controller_name> extends MY_Controller {

你完成了

另请注意,要在视图中使用的所有变量都在此变量(array) $this->data

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 :(得分:1)

你不是简短的回答。

MVC的目的是让您的代码井井有条。

您可以做的是在libraries文件夹中创建一个库,您可以在多个控制器中放置所需的方法。

例如,您可以创建一个mylog库,您可以在其中放置所有与日志相关的内容。在任何控制器中,您将调用:

$this->load->library('mylog');
$this->mylog->logdata();

除了处理数据模型的函数应该驻留在模型中。您可以从CI中的任何控制器调用任何模型

答案 2 :(得分:0)

这是概念性的,如果你想在不同的控制器中调用代码,请执行以下操作:

  1. 随时(甚至在视图中)创建帮助和调用函数。
  2. 扩展CI_Controller,以便您可以在任何扩展的控制器中使用一个或多个函数。

  3. 让我们从助手开始:

    在文件夹application/helpers/summation_helper.php

    中创建文件

    使用以下代码示例

    <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
    
    function sum($a, $b) {
    
        //$CI =& get_instance(); 
        // if you want to use $this variable you need to get instance of it so now instead of using $this->load... you may use $CI->load
        $return = $a + $b;
        return number_format($return, 3);
    
    }
    

    如果您要在许多控制器/视图中使用您的助手,请在自动加载中加载,否则只需手动加载$this->load->helper('summation');

    扩展Controller_CI:如果您使用数据库,这是更好的方法。请按照此tutorial进行解释。

    *我已经在网站发布此消息之前制作了一个答案。