簡體   English   中英

在ay控制器函數codeigniter中調用x控制器函數

[英]calling x controller function inside a y controller function codeigniter

我是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調用函數。 我該怎么做呢? 謝謝。

這里參考

要擴展控制器,請遵循本教程或在下面查看一些代碼。


私人/公共/受保護者之間的區別


在文件夾/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擴展的某些控制器的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
    }

}

答案不是很簡單。

MVC的目的是使您的代碼井井有條。

但是,您可以做的是在libss文件夾中創建一個庫,將所需的方法放置在多個控制器中。

例如,您可以創建一個mylog庫,您可以在其中放置所有與日志相關的內容。 然后,在任何控制器中,您將調用:

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

此外,處理數據模型的功能應駐留在模型中。 您可以從CI中的任何控制器調用任何模型

如果您想在不同的控制器中調用代碼,那是不可行的,請執行以下操作:

  1. 隨時隨地(甚至在視圖中)創建幫助程序並調用函數。
  2. 擴展CI_Controller,因此您可以在任何擴展的控制器中使用一個或多個功能。

讓我們從助手開始:

在文件夾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 :如果使用數據庫,這是一種更好的方法。 請按照本教程進行說明。

*我在網站從手機上發布此消息之前就精心設計了一個答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM