2016-02-03 49 views
5

您好,我正在嘗試從Codeigniter中的所有CONTROLLERS中獲取所有函數名稱 我能夠獲取數組中的所有CONTROLLER名稱,但未能獲取所有控制器的所有函數。我只接收當前控制器的函數名稱,我正在寫這個函數。獲取Codeigniter中所有CONTROLLERS的所有函數名稱

我被$class_methods=get_class_methods(new classname());

取回功能名稱。如果我嘗試了全球,我得到目錄錯誤。

+0

http://www.stackoverflow.com/questions/5919546/how-to-list-all-controller-class-name-in-codeigniter – devpro

回答

3

對於所有控制器,其方法使用該庫無濟於事名單:

<?php 
if (!defined('BASEPATH')) 
    exit('No direct script access allowed'); 
/*** 
* File: (Codeigniterapp)/libraries/Controllerlist.php 
* 
* A simple library to list all your controllers with their methods. 
* This library will return an array with controllers and methods 
* 
* The library will scan the "controller" directory and (in case of) one (1) subdirectory level deep 
* for controllers 
* 
* Usage in one of your controllers: 
* 
* $this->load->library('controllerlist'); 
* print_r($this->controllerlist->getControllers()); 
* 
* @author Peter Prins 
*/ 
class ControllerList { 

    /** 
    * Codeigniter reference 
    */ 
    private $CI; 

    /** 
    * Array that will hold the controller names and methods 
    */ 
    private $aControllers; 

    // Construct 
    function __construct() { 
     // Get Codeigniter instance 
     $this->CI = get_instance(); 

     // Get all controllers 
     $this->setControllers(); 
    } 

    /** 
    * Return all controllers and their methods 
    * @return array 
    */ 
    public function getControllers() { 
     return $this->aControllers; 
    } 

    /** 
    * Set the array holding the controller name and methods 
    */ 
    public function setControllerMethods($p_sControllerName, $p_aControllerMethods) { 
     $this->aControllers[$p_sControllerName] = $p_aControllerMethods; 
    } 

    /** 
    * Search and set controller and methods. 
    */ 
    private function setControllers() { 
     // Loop through the controller directory 
     foreach(glob(APPPATH . 'controllers/*') as $controller) { 

      // if the value in the loop is a directory loop through that directory 
      if(is_dir($controller)) { 
       // Get name of directory 
       $dirname = basename($controller, EXT); 

       // Loop through the subdirectory 
       foreach(glob(APPPATH . 'controllers/'.$dirname.'/*') as $subdircontroller) { 
        // Get the name of the subdir 
        $subdircontrollername = basename($subdircontroller, EXT); 

        // Load the controller file in memory if it's not load already 
        if(!class_exists($subdircontrollername)) { 
         $this->CI->load->file($subdircontroller); 
        } 
        // Add the controllername to the array with its methods 
        $aMethods = get_class_methods($subdircontrollername); 
        $aUserMethods = array(); 
        foreach($aMethods as $method) { 
         if($method != '__construct' && $method != 'get_instance' && $method != $subdircontrollername) { 
          $aUserMethods[] = $method; 
         } 
        } 
        $this->setControllerMethods($subdircontrollername, $aUserMethods);          
       } 
      } 
      else if(pathinfo($controller, PATHINFO_EXTENSION) == "php"){ 
       // value is no directory get controller name     
       $controllername = basename($controller, EXT); 

       // Load the class in memory (if it's not loaded already) 
       if(!class_exists($controllername)) { 
        $this->CI->load->file($controller); 
       } 

       // Add controller and methods to the array 
       $aMethods = get_class_methods($controllername); 
       $aUserMethods = array(); 
       if(is_array($aMethods)){ 
        foreach($aMethods as $method) { 
         if($method != '__construct' && $method != 'get_instance' && $method != $controllername) { 
          $aUserMethods[] = $method; 
         } 
        } 
       } 

       $this->setControllerMethods($controllername, $aUserMethods);         
      } 
     } 
    } 
} 
// EOF 

將它保存在庫文件夾

比這個庫加載到你的控制器現在

$this->load->library('controllerlist'); 

print_r($this->controllerlist->getControllers()); 

你會使用其方法獲取所有控制器列表。

如果您有任何問題,請問我。

+0

+1爲您的偉大圖書館,謝謝。但是我認爲它需要在類外部定義('EXT','。php');否則它會顯示未定義的EXT錯誤。我嘗試了一次,但它失敗了,所以在我添加該行後,代碼有效(並且我不知道它爲什麼起作用) – poring91

+0

是的,您需要在根目錄的索引頁上使用此行。 –

相關問題