For a normal shared config file, create a setup object and load it:
use Quantum\Loader\Loader;
use Quantum\Loader\Setup;
$loader = new Loader();
$config = $loader
->setup(new Setup('config', 'database'))
->load();With default hierarchical behavior, resolution is:
modules/<current-module>/config/database.php(if a current module exists)config/database.php(primary path)shared/config/database.php(fallback)
If your project keeps config under shared/config, Loader reaches it through step 3.
If a package can work without a file, check first:
$setup = new Setup('config', 'uploads');
$loader = new Loader();
$loader->setup($setup);
if ($loader->fileExists()) {
$uploads = $loader->load();
}This is the pattern Quantum uses for optional uploads configuration.
To let a module override a shared resource, keep hierarchical loading enabled and set the module explicitly when needed:
$setup = new Setup('config', 'auth', true, 'Admin');
$config = (new Loader())
->setup($setup)
->load();Resolution order is:
modules/Admin/config/auth.phpshared/config/auth.php
If a file must exist only inside a module, turn hierarchy off:
$setup = new Setup('views', 'dashboard', false, 'Admin');
$loader = new Loader();
$loader->setup($setup);
if (!$loader->fileExists()) {
// handle the missing module file yourself
}With hierarchical set to false, the loader does not look under shared/.
If you later want to load the shared version again, create a new Setup. setModule() only accepts a string, so reusing the same Setup instance does not give you a built-in way to clear the module override.
pathPrefix is optional. When you omit it, Loader resolves a file from the project root instead of a subdirectory:
$setup = new Setup(null, 'routes', false, null, 'routes.php is missing.');
$routes = (new Loader())
->setup($setup)
->load();That resolves routes.php from the base path returned by App::getBaseDir().
getFilePath() and load() use the exception message stored in Setup:
$setup = new Setup(
'config',
'mailer',
true,
null,
'Mailer configuration is missing.'
);
$config = (new Loader())
->setup($setup)
->load();Use this when a generic File config/mailer not found! message is too low-level for your package.
Use loadDir() when you want every PHP helper file in one directory pattern to be included:
$loader = new Loader();
$loader->loadDir(base_dir() . DS . 'helpers');
$loader->loadDir(base_dir() . DS . 'modules' . DS . '*' . DS . 'helpers');A few caveats matter here:
- only
*.phpfiles are included - subdirectories are ignored
- files are included with
require_once Setupis not used for directory loading
- Shared fallback lowercases
pathPrefix; module/primary paths use the value as provided. Use consistent lowercase directory names to avoid case-related surprises across environments. load()returns whatever the target PHP file returns. If the file returns nothing, you get PHP's normalrequirereturn value.setup()copies values fromSetupintoLoader. If you mutate theSetupobject afterward, callsetup()again before loading.- Reuse a
Setupobject only when you want the same resolution rules. It is mutable and can be changed after construction. - Reuse a DI-managed
Loadercarefully. Always callsetup()again before each new load.