CodeIgniter - How to get a list of all my controll

2019-02-20 14:05发布

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.

2条回答
The star\"
2楼-- · 2019-02-20 14:26
foreach(glob(APPPATH . 'controllers/*' . 'php') as $controller){
   $controller = basename($controller, '.php');
}
查看更多
一纸荒年 Trace。
3楼-- · 2019-02-20 14:46

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

查看更多
登录 后发表回答