How to list all controller class name in codeigniter?

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

Thanks in advance,

Logan

+3
source share
2 answers

The best thing is to explicitly write them to the new configuration file.

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

Otherwise, you will scan directories that will consume resources. But you can 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 the file names match the names of the controllers, you should now have an array of all your controllers. This is not ideal, although for several reasons, but should work for most settings.

, , , . - , .

, , , .

+8

, , , , , :

interface IAuthorizationRequired
{
     public function __auth();
}

()

class BlogController extends CI_Controller implements IAuthorizationRequired
{
     public function __auth()
     {
          /*Redirect or Custom*/
     }
}

:

if(($controller instanceof IAuthorizationRequired) && method_exists(array($controller,'__auth')))
{
     $authed = $controller->__auth();
     if(!$authed)
     {
         echo 'Authorization Failed';
         exit;
     }
}

2.0 __auth, auth.

0

All Articles