CodeIgniter Forums
Modules not loading config files - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forum-6.html)
+--- Forum: Issues (https://forum.codeigniter.com/forum-19.html)
+--- Thread: Modules not loading config files (/thread-78863.html)

Pages: 1 2


Modules not loading config files - ZoeF - 03-19-2021

I am wondering

https://forum.codeigniter.com/thread-78802.html

In the above topic I asked for the best practices for the loading of config files inside of a module? I have the module loaded in the autoload, I have the auto discovery option enabled. And I have the constants added to the Modules.php $aliases array in the main app config folder. 

Yet the constants are not beeing found. So I cannot use them in the application. I did find a work around but that means changing the main Constants.php by adding the following code.

PHP Code:
/**
 * --------------------------------------------------------------------
 * Include Modules Constants Files
 * --------------------------------------------------------------------
 */

if (file_exists(ROOTPATH'modules')) {
    
$modulesPath ROOTPATH.'modules/';
    
$modules scandir($modulesPath);

    foreach (
$modules as $module) {
        if (
$module === '.' || $module === '..') continue;
        if (
is_dir($modulesPath) . '/' $module) {
            
$constantsPath $modulesPath $module '/Config/Constants.php';
            if (
file_exists($constantsPath)) {
                require(
$constantsPath);
            } else {
                continue;
            }
        }
    }


I do however stil wonder if I missed something in the documentation. Or that is actualy a bug.


RE: Modules not loading config files - kenjis - 03-19-2021

(03-19-2021, 12:42 PM)ZoeF Wrote: And I have the constants added to the Modules.php $aliases array in the main app config folder. 

Yet the constants are not beeing found.

There is no aliases `constants`.
Code:
        'events',
        'filters',
        'registrars',
        'routes',
        'services',
That's all.

By the way, why do you need to use constants?
You could use Config class and class constants.


RE: Modules not loading config files - ZoeF - 03-19-2021

There are many ways of adding constants indeed. But maybe I want the constants to be for my hole project if I add a specific module.

The most logical answer I came across is that the constants file is not a class file. That however should not be a excuse to exclude a config file from this system.


RE: Modules not loading config files - InsiteFX - 03-20-2021

Modules do not autoload the config files in them you have to load them.

If you need constants then add them in the app/Common.php then they will be there.

And here is the way I tested it.

app/Common.php
PHP Code:
<?php

/**
 * The goal of this file is to allow developers a location
 * where they can overwrite core procedural functions and
 * replace them with their own. This file is loaded during
 * the bootstrap process and is called during the frameworks
 * execution.
 *
 * This can be looked at as a `master helper` file that is
 * loaded early on, and may also contain additional functions
 * that you'd like to use throughout your entire application
 *
 * @link: https://codeigniter4.github.io/CodeIgniter4/
 */

/**
 * My Defined Constants
 * -----------------------------------------------------------------------
 * Test
 */
defined('APP_TEST') || define('APP_TEST''Test'); 



RE: Modules not loading config files - ZoeF - 03-20-2021

Guess it does not work when the file is oudside of the app folder, but why would I add constants in the Common.php of the main app folder for a module that is loaded in in the Modules direcrory? Why is it so hard to add constants in a specific module? Guess I will need to use the crude solution.

directory structure
>app
>modules
->Admin (I want constants in here)
->Users
->...
>public
>vendor
>...


RE: Modules not loading config files - kenjis - 03-20-2021

> Why is it so hard to add constants in a specific module?

Probably because there is no way to autoload constants in PHP.

I personally recommend not to use global constants at all.


RE: Modules not loading config files - ZoeF - 03-20-2021

Global constants could be handy for allowing hole portions of the application to either work or not work when a module is loaded or not? If a constant is present do this -> else to this. Seems like a straight forward thing. I believe their might be diffrent solutions to the idea. And I am shure I wil get an opinion from someone why this is not a good idea.

But that is beside the question.


RE: Modules not loading config files - InsiteFX - 03-20-2021

You can always place them in your own php file and include it were needed.


RE: Modules not loading config files - ZoeF - 03-21-2021

Again besides the point. Why is it possible to load routes.php when it is inside a modules directory and not constants? Very strange thinking.


RE: Modules not loading config files - paulbalandan - 03-26-2021

Autoloading constants file, as well as other non-class files, will be a new feature for the Autoloader class. This will be implemented by autoloading "files", as opposed to autoloading classes.

Are you using Composer? If yes, then there is a workaround. In your composer.json, add the files array to your autoload section.
Code:
"autoload": {
    "files": [
        "path/to/constants.php"
    ]
}

Then in your terminal, just run ` composer dump-autoload`