douji9816 2011-05-07 06:39
浏览 32
已采纳

如何在codeigniter中列出所有控制器类名?

I want to create user authentication with access level for my site back end. I want get and list all controller class name for create user group creation.

Thanks in advance,

Logan

  • 写回答

2条回答 默认 最新

  • doulang6695 2011-05-07 08:00
    关注

    Your best bet for this is to explicitly write them into a new config file.

    $config['controllers'] = array(
        'blog',
        'events',
        'news', // etc.
    );
    

    Otherwise, you will be scanning directories which will eat up resources. But you could do it like this:

        $controllers = array();
        $this->load->helper('file');
    
        // Scan files in the /application/controllers directory
        // Set the second param to TRUE or remove it if you 
        // don't have controllers in sub directories
        $files = get_dir_file_info(APPPATH.'controllers', FALSE);
    
        // Loop through file names removing .php extension
        foreach (array_keys($files) as $file)
        {
            $controllers[] = str_replace(EXT, '', $file);
        }
        print_r($controllers); // Array with all our controllers
    

    Since file names match controller names, now you should have an array of all your controllers. This isn't perfect though for several reasons, but should work for most setups.

    Personally I use a highly modified directory structure, so this wouldn't work for me, and some controllers I would want to ignore as well. Another option would be to cache the results to a file, but that's a separate tutorial.

    I highly suggest defining them in a config file, this way you can store other useful information directly related to your access control and avoid the huge overhead of recursively scanning directories.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端