【问题标题】:CodeIgniter get all controller list with its methodCodeIgniter 用它的方法获取所有控制器列表
【发布时间】:2016-06-21 06:40:43
【问题描述】:

我想根据控制器方法创建用户角色和权限。为此,我需要一个控制器列表及其方法。

如果我将一个方法添加到任何类中,我希望它动态地在此列表中列出

为此,我在我的方法中尝试了这个,但问题是它只显示当前控制器及其方法。:

<?php
$controller = $this->router->fetch_class();
$method = $this->router->fetch_method();
?>

请帮忙。

提前致谢

【问题讨论】:

标签: codeigniter codeigniter-3


【解决方案1】:

使用这个库通过你的方法获取所有 Codeigniter 类。在 application/config/autoload.php 上,您需要加载 Controllerlist

<?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;
    private $EXT;

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

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

        // 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, $this->CI->EXT);

                // Loop through the subdirectory
                foreach(glob(APPPATH . 'controllers/'.$dirname.'/*') as $subdircontroller) {
                    // Get the name of the subdir
                    $subdircontrollername = basename($subdircontroller, $this->CI->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, $this->CI->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

【讨论】:

  • 感谢您提供此代码。它对我的项目非常有效。出于我的目的,我可以将控制器列为用户应用程序,将功能列为可分配的工具。我喜欢你不包括私人功能。为了隐藏函数,我在它们前面加上一个像“a_”这样的字符集,并使用一个正则表达式来跳过它们。 /^(index)|^\_|^([a-z]\_)/
猜你喜欢
  • 2015-08-23
  • 2015-04-29
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-20
  • 1970-01-01
  • 2015-08-01
相关资源
最近更新 更多