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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀