如何从HMVC Codeigniter中列出所有类和方法?

时间:2018-12-11 07:38:56

标签: php arrays codeigniter hmvc

我的modules中有一些HMVC,是否可以将classes目录中的所有methodsmodules列出并放入一个数组?

谢谢,抱歉我的英语不好

2 个答案:

答案 0 :(得分:0)

尝试此..创建一个名为“ Controllerslist”的库,然后复制此代码..

class Controllerslist {


    private $CI;


    private $aControllers;


    function __construct() {

        $this->CI = get_instance();

        $this->setControllers();
    }


    public function getControllers() {
        return $this->aControllers;
    }


    public function setControllerMethods($p_sControllerName, $p_aControllerMethods) {
        $this->aControllers[$p_sControllerName] = $p_aControllerMethods;
    }

    private function setControllers() {

        foreach(glob(APPPATH . 'modules/controllers/*') as $controller) {


            if(is_dir($controller)) {

                $dirname = basename($controller);


                foreach(glob(APPPATH . 'modules/controllers/'.$dirname.'/*') as $subdircontroller) {

                    $subdircontrollername = basename($subdircontroller, EXT);


                    if(!class_exists($subdircontrollername)) {
                        $this->CI->load->file($subdircontroller);
                    }

                    $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"){

                $controllername = basename($controller, EXT);


                if(!class_exists($controllername)) {
                    $this->CI->load->file($controller);
                }


                $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);                                
            }
        }   
    }
}

在此之后..在您的控制器中加载该库

$this->load->library('Controllerslist');
$list = $this->controllerslist->getControllers();
print_r($list);

这对我有用..

答案 1 :(得分:-1)

小更新Utkarsh Tiwari 对于HMVC结构,我们应该首先获取所有模块列表,然后获取每个模块的所有控制器..

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() {

        foreach(glob(APPPATH . 'modules/*') as $modules_all) {

            if(is_dir($modules_all)) {

                $dirname = basename($modules_all);

            foreach(glob(APPPATH . 'modules/'.$dirname.'/controllers/*') as $subdircontroller) {

                    $subdircontrollername = basename($subdircontroller, EXT);


                    if(!class_exists($subdircontrollername)) {
                        $this->CI->load->file($subdircontroller);
                    }

                    $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"){

                $controllername = basename($controller, EXT);


                if(!class_exists($controllername)) {
                    $this->CI->load->file($controller);
                }


                $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);                                
            }
        } 
    }
}

在此之后,您可以简单地获取所有方法和控制器列表。

$this->load->library('controllerlist');
$list = $this->controllerlist->getControllers();
print_r($list);

这对我有用。