2016-02-03 46 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

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

相关问题