CodeIgniter用它的方法获取所有 Controller 列表

标签 codeigniter codeigniter-3

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

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

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

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

请帮忙。

提前致谢

最佳答案

使用此库可以使用您的方法获取所有 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

关于CodeIgniter用它的方法获取所有 Controller 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37937198/

相关文章:

php - SMTP 邮件未发送 - Codeigniter 电子邮件库

php - 消息 : Class 'Illuminate\Database\Capsule\Manager' not found in Codeigniter3. 1

javascript - Ajax 调用在 CodeIgniter 中不起作用

php - 我无法使用 codeigniter 进行简单的数据库更新

php - 如何计算表中的值并返回 true,仅当使用 php mysql 找到该值 5 次时

php - 使用 Codeigniter 3 进行多站点 |数据库自动切换

php - Codeigniter 在名为 'State' 的对象属性上失败

xml - USPS api - 使用curl和codeigniter

mysql - 如何在 codeigniter 中运行自定义更新查询

javascript - 循环遍历 MVCArray 以将纬度和经度保存在数据库中