-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Need a global way to set config values dynamically #1661
Comments
Things can get even more complicated ... wanting to set config options (locale or ???) from a database table :-/ |
That's already been asked for :) But it makes sense what they're saying here, though I'm not sure if what that particular post is asking for is even possible within the controller, and In don't think it is. I gave them a potential solution in the forums, but I still think we should investigate what's possible here and at least add to the docs the caveats and limitations. |
What about to instruct them to use the Config's constructors? The thread has a post with a Controller where it wants to change the locale on the fly, but the Service already is loaded. Maybe it must be done via For the database use case, it could to use the constructors. But, too many SQL queries needed, I think. Maybe an hook, Event, available just after the DotEnv load. CodeIgniter4/system/bootstrap.php Lines 139 to 142 in fa5549f
Events::trigger('config'); // pre-pre_system ?
The first thought is that it will continues needing to use In the Event function, the developer could do a loop setting the items given from a database table, cache, API, etc. Then, all configurations are set when the app run. |
Database Table:
app/Config/Events.php: Events::on('config', function () {
$configs = Database::connect(...)
->query('SELECT * FROM config_table WHERE site_id = "x"')
->getResult();
foreach($configs as $config)
{
config($config->class)->{$config->property} = $config->value;
}
}); Where the site_id can be a custom Most important would be the "config" Event. Executed in the bootstrap file, before the current |
As far as that specific post goes - I forgot that IncomingRequest has a |
This is already referred to #1618 and under milestone of 4.1.0 |
@jim-parry, can you move this to 4.1.0 as similar issue is already mentioned in #1618 |
Why did you choose classes as configs? /* app/config/file.php
* return [
* 'key' => [
* 'subkey' => 'value',
* 'subkey2' => env('subkey2', 'default'), // load from .env
* ],
* ];
*/
config('file.key.subkey') // get value
config('file.key.subkey', 'newValue') //set value |
Was? It's never been that way in CI. That's Laravel you're thinking of. |
oh, sorry. "will be" ^_^ |
Well - this isn't Laravel. :) Yes, that works nicely, but we had different goals 3 years ago when we made that decision to use classes for a number of reasons. Not going to change at this point. |
This would be perfect for my case. I need to set |
Currently it's intended that config values are either set in the config file, the .env file, or controller sets an instance and passes it to chlldren. Need to be able to set a config value dynamically and have it set for all cached instances.
This became apparent in this thread, where they were trying to set the language at runtime and the wrong language was being used.
Could be that it needs to be handled in a hook prior to negotiation happens, but need to investigate and make sure it works otherwise.
The text was updated successfully, but these errors were encountered: