How to get an array of all controllers in a Codeigniter project?

前端 未结 1 1800
梦谈多话
梦谈多话 2021-01-14 22:10

I\'d like to obtain a list of all controllers in a Codeiginiter project so I can easily loop through each of them and add defined routes. I can\'t seem to find a method tha

相关标签:
1条回答
  • 2021-01-14 22:46

    Well to directly answer to coding question, you can do this:

    foreach(glob(APPPATH . 'controllers/*' . EXT) as $controller)
    {
        $controller = basename($controller, EXT);
    
        $route[$controller] = $controller . '/index';
        $route[$controller . '/(.+)'] = $controller . '/$1';
    }
    

    Buuuuuut this may not work out to be the most flexible method further down the line.

    There are a few other ways to do it. One is to create a MY_Router and insert

    $this->set_class('pages'); 
    $this->set_method($segments[0]);
    

    before/instead of show_404();

    That will send /contact to /pages/contact, but only if no controllers, methods, routes are mapped to first.

    OOOOOOORRRRRR use Modular Separation and add the following to your main routes.php

    $routes['404'] = 'pages';
    
    0 讨论(0)
提交回复
热议问题