获取Codeigniter中所有控制器的所有FUNCTIONS名称 [英] Get all FUNCTIONS name from all CONTROLLERS in Codeigniter

查看:622
本文介绍了获取Codeigniter中所有控制器的所有FUNCTIONS名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello我试图从Codeigniter的所有控制器的所有FUNCTIONS名称
我可以得到所有的控制器名称在数组,但未能获得所有控制器的所有功能。

Hello I am trying to get all FUNCTIONS name from all CONTROLLERS in Codeigniter Well I am able to get all CONTROLLER names in array but am failed to get all functions of all controllers. I only get functions names of current controller in which I am writing the function.

我通过获取函数名称$ class_methods = get_class_methods(new classname ());

如果我在全局范围内尝试它,我会得到目录错误。

If I tried it globally I get directory error.

推荐答案

所有控制器的可用列表及其方法使用此库:

For avail list of all controller with its method use this library:

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


b $ b

将其保存到库文件夹

Save it in library folder

将此库加载到控制器中

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

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

现在您将获得所有控制器列表及其方法。

Now you will get all controller list with its method.

如果您有任何问题,请问我。

if you have any question please ask me.

这篇关于获取Codeigniter中所有控制器的所有FUNCTIONS名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆