如何列出codeigniter中的所有控制器类名? [英] How to list all controller class name in 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.
提前感谢,
推荐答案
一个新的配置文件。
$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.
这篇关于如何列出codeigniter中的所有控制器类名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!