Welcome Guest, Not a member yet? Register   Sign In
Autoloading of configs in cascade
#1

Hello everyone,
I am trying to use CI4 in way that I need to autoload config files from common app folder. My scenario is that I want to have multiple applications that act as the main app but I want to use config files from the common app folder. I would like to have it autoloaded in cascade. Firstly, the application would load necessary configs in own folder like app, paths, routes then rest would be loaded from common app folder but only in the case that application folder doesn't have own configs, that means configs in application folder are superior to those in common config folder.

Why all of this? Idea is to reduce files and in case of a change in common configs, I do not have to overwrite all my application configs but only one.

THX
Reply
#2

There's no solution for that exact situation at the moment. The simplest thing to do would be to make an empty class for each config file in the main application. Since config files are classes now they can extend the config files in the Common folder. If you need to add or modify things in the application config file, you can overwrite in that class file and it should all work just fine.

The other option is to create your own version of the config() function that handles that for you.
Reply
#3

(This post was last modified: 07-22-2019, 09:29 AM by JoellSVK.)

(07-21-2019, 07:09 PM)kilishan Wrote: There's no solution for that exact situation at the moment. The simplest thing to do would be to make an empty class for each config file in the main application. Since config files are classes now they can extend the config files in the Common folder. If you need to add or modify things in the application config file, you can overwrite in that class file and it should all work just fine.

The other option is to create your own version of the config() function that handles that for you.

Thank You for your quick answer. I had the same idea with empty extending classes but still, I was looking for a better way how to handle it. Is possible to bypass it without editing framework files? I would make my own function but now I am still learning how the framework works. I was checking the flow and it seems config function is defined in common.php (what is not class) and it is called from bootstrap.php that is the same, it is not class so extending these two is not an option. After that, we have "get" function in Config class, that class can be extended but the framework will recognise extending it or how it can be done?

Thank You

Edit:// I was researching all day this topic and it seems from my point very hard to make something to bypass loading classes since they are used across framework in many places and also like mentioned in bootstrap and common as well. Maybe I am wrong and I was trying to extend core files but IDE doesn't like extending class by itself like it is written in the guide. Maybe someone can bring idea how to do it? Smile i am quite out of ideas now....
Reply
#4

Yeah, there's a tricky situation of overriding the functions within Common.php. For many things you can override within app/Config/Boot/{environment}.php (replacing environment for the name of the current environment, like development, testing, etc). But the Common.php file is needed prior to loading those boot files, unfortunately.

You don't want to bypass loading a core file. For this example, you would do a two-step process:

1. Create a new class MyConfig (or whatever) that extends CodeIgniter\Config\Config class.
2. Update the get() method to work like you want it to.
3. Use public/index.php to load a file that contains your config() function.

Don't consider public/index.php as a framework file - that's for you to modify if needed. Of course you're responsible if you break things at that point Smile

All of the functions in Common.php will first check to make sure that function doesn't exist before trying to define it. So your custom config() function will be used throughout the framework now.

This does appear to be an area that needs a little work. I think checking for the existing of an app/Config/Functions.php file or something in the bootstrap would do it. Developers can modify that file then without breaking anything. So - thanks for bringing it up! I'll make an issue for that now.
Reply
#5

Thank You very much for your help in this matter. I will try to now make it together and I will tell you the result of how I did it.

+1 Support
Reply




Theme © iAndrew 2016 - Forum software by © MyBB