• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Modules not loading config files

#1
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.
Reply

#2
(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.
Reply

#3
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.
Reply

#4
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'); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#5
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
>...
Reply

#6
> 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.
Reply

#7
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.
Reply

#8
You can always place them in your own php file and include it were needed.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#9
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.
Reply

#10
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`
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.