CodeIgniter Forums
Automatically load all config files (HMVC and subfolders) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Automatically load all config files (HMVC and subfolders) (/showthread.php?tid=50195)



Automatically load all config files (HMVC and subfolders) - El Forum - 03-18-2012

[eluser]Noobigniter[/eluser]
Hello,

I want to know if there is no better way to do what I'm doing exactly.

I use a HMVC with modules and subfolders, everything works fine but each time the load configuration files is a daunting task to me.
That's why I want to automatically load all the files in the folder modules configs AND subfolders.

The first problem is that I have to copy this which is already in my application/config/config.php :
Code:
$config['modules_locations'] = array(
APPPATH.'/modules/',
APPPATH.'/modules/community/',
);

The second problem is that I have to put my code in the file application/config/routes.php, otherwise it is impossible to properly use this file routes.php.

Content of my application/config/routes.php
Code:
$rtlg = '(\w{2})'; # routing lang (de|en|fr|it|nl|ru)

$route[$rtlg] = $route['default_controller'] = 'page'; # '/en', '/fr', etc…  URIs -> use default controller  

$config['modules_locations'] = array(
APPPATH.'/modules/',
APPPATH.'/modules/community/',
);

// get all config files present in (array) $config['modules_locations']
foreach($config['modules_locations'] as $routing) :
if (is_dir($routing))
  if ($handle = opendir($routing))
   while (FALSE !== ($module = readdir($handle)))
    if ((substr($module, 0, 1) != '.') AND (substr($module, 0, 2) != '..'))
     if (is_dir($dir = $routing.$module.'/config/'))
      if ($handle2 = opendir($dir))
       while (FALSE !== ($file = readdir($handle2)))
        if ((substr($file, 0, 1) != '.') AND (substr($file, 0, 2) != '..') AND (substr($file, -4) == EXT))
         if (file_exists($is_file = $dir.$file))
          require_once($is_file);
      closedir($handle2);
  closedir($handle);
endforeach;

$route[$rtlg.'/(.+)'] = "$2";
$route['404_override'] = '';

Do not there no way around it?
If you have any ideas, let me know Smile


In advance thank you.


Automatically load all config files (HMVC and subfolders) - El Forum - 03-19-2012

[eluser]Aken[/eluser]
You can autoload config files in config/autoload.php


Automatically load all config files (HMVC and subfolders) - El Forum - 03-21-2012

[eluser]Noobigniter[/eluser]
Hello,

Thank you for taking the time to respond.

I tried, but it's not really what I want, actually I was trying to have all files of each module, in itself, but it is apparently not possible, at least with routes.php files.