CodeIgniter - 如何动态获取我的所有控制器的列表? [英] CodeIgniter - How to get a list of all my controllers dynamically?

查看:117
本文介绍了CodeIgniter - 如何动态获取我的所有控制器的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要运行一个函数来对我的项目中的每个控制器名称做一些操作。
我的函数是在这样的控制器上定义的:

 class $ Some_Controller extends CI_Controller { 
public function someActions(){
// $ listOfAllControllers = some_method_I_need_for_my_answer();
foreach($ listOfAllControllers as $ controllerName){
// some_action($ controllerName)
}
}
}



我想要的是我的项目中存在的所有控制器的动态列表


< 您需要扫描您的 / application / controllers目录并从中删除文件扩展名

  $ controllers = array(); 
$ this-> load-> helper('file');

//扫描/ application / controllers目录中的文件
//将第二个参数设置为TRUE,或者如果
//在子目录中没有控制器,则删除它
$ files = get_dir_file_info(APPPATH.'controllers',FALSE);

//通过文件名循环删除.php扩展名
foreach(array_keys($ files)as $ file){
if($ file!='index.html')
$ controllers [] = str_replace('。php','',$ file);
}
print_r($ controllers); //所有控制器的数组



您也可以点击此链接实现此



控制器列表


I need to run a function to do some actions with each controller-name on my project. My function is defined on a controller like this:

class Some_Controller extends CI_Controller{
    public function someActions(){
        // $listOfAllControllers = some_method_I_need_for_my_answer();
        foreach($listOfAllControllers as $controllerName){
            // some_action($controllerName)
        }
    }
}

What I want is a dynamic list of all controllers which exists in my project.

解决方案

You need to scan your /application/controllers directory and remove file extension from it

    $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 ) {
        if ( $file != 'index.html' )
            $controllers[] = str_replace('.php', '', $file);
    }
    print_r($controllers); // Array with all our controllers

OR

You can also follow this link to achieve this

controller list

这篇关于CodeIgniter - 如何动态获取我的所有控制器的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆