I made this loader which actually works pretty well. I now want to make my application completely modular. This will allow developers to overwrite individual bits of functionality and more easily extend the application.
I came up with this originally.
// Try to load a module
$file_path = $segments[1] . '/' . $segments[2] . '/' . ucwords($segments[2]) . '.php';
$paths = array (
'third-party' => SYS . 'extensions/third-party/modules/',
'core' => SYS . 'extensions/core/modules/'
);
foreach ( $paths as $path) {
if ( file_exists($path . $file_path)) {
require($path . $file_path);
if ( class_exists($segments[2])) {
$module = new $segments[2];
$function = isset($segments[3]) ? $segments[3] : 'index';
if (method_exists($module, $function)) {
$variables = $this->Router->get_variables(3);
// Define Module Path
define("MODULE_PATH", $path . $segments[1] . '/' . $segments[2] . '/');
return call_user_func_array(array($module, $function), $variables);
}
}
}
}
It works as follows.
If you enter admin/user into the url. In an earlier part of my script it looks for the admin class. If it finds the admin class it looks for the user function. If the user function cannot be found. The code above is called to look for a module.
This module will be found in.
xxx/modules/admin/user/User.php
The code above checks to see if this file exists, checks to see if the class exists, it determines what function should be called, it checks to see if the function exists within the class, then it calls the function.
What I want to do is check for any level of this.
For example.
?admin/user/register
This would be found at.
xxx/modules/admin/user/register/Register.php
Does anyone know what the best way is to adapt the code above to find any depth of route?
Your best bet would be to (on init) go through all the files in your target directories (id est those where modules COULD be), and make an index array, which will look something like this (for myFile.php, assuming it is in “modules/something/myDir/mySubdir/):
Now, when you have got a path like the example you provided (?admin/user/register), use this function, it should find a match in the files, assuming they are stored in and array, with keys that have a value equal to their filename:
You might want to save the index for the files, and refresh it only when something was changed, for performance.
Also, I have not tested the above code, so there might be a small flaw, or maybe I have not used an API function where I could. Oh well. I think you will get the idea.