Skip to content
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

Closed
lonnieezell opened this issue Jan 11, 2019 · 12 comments
Closed

Need a global way to set config values dynamically #1661

lonnieezell opened this issue Jan 11, 2019 · 12 comments
Labels
Milestone

Comments

@lonnieezell
Copy link
Member

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.

@jim-parry
Copy link
Contributor

Things can get even more complicated ... wanting to set config options (locale or ???) from a database table :-/
The reasons make sense, but the reasoning could be circular :-S

@lonnieezell
Copy link
Member Author

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.

@natanfelles
Copy link
Contributor

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 \Config\Services::language()->setLocale('ru') and not by a config property.


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.

$env = new \CodeIgniter\Config\DotEnv(ROOTPATH);
$env->load();

Events::trigger('config'); // pre-pre_system ?

The first thought is that it will continues needing to use config('Foo')->var = 'bar';.

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.

@natanfelles
Copy link
Contributor

Database Table:

site_id class property value
x App baseURL site1.com
x App defaultLocale kgp

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 $_SERVER var, the Host, etc.


Most important would be the "config" Event. Executed in the bootstrap file, before the current pre_system. (Or the pre_system could be moved?)

@lonnieezell
Copy link
Member Author

As far as that specific post goes - I forgot that IncomingRequest has a setLocale() method.

@jim-parry jim-parry added this to the 4.0.0-beta.2 milestone Mar 5, 2019
@jim-parry jim-parry added the dev label Mar 6, 2019
@atishhamte
Copy link
Contributor

This is already referred to #1618 and under milestone of 4.1.0

@atishhamte
Copy link
Contributor

@jim-parry, can you move this to 4.1.0 as similar issue is already mentioned in #1618

@lonnieezell lonnieezell modified the milestones: 4.0.0-beta.2, 4.1.0 Mar 26, 2019
@iRedds
Copy link
Collaborator

iRedds commented May 17, 2019

Why did you choose classes as configs?
I think it will be easier to use arrays with dot notation.

/* 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

@lonnieezell
Copy link
Member Author

Why did you choose classes as configs?
I think it was easier to use arrays with dot notation.

Was? It's never been that way in CI. That's Laravel you're thinking of.

@iRedds
Copy link
Collaborator

iRedds commented May 18, 2019

oh, sorry. "will be" ^_^
Yes. Laravel.

@lonnieezell
Copy link
Member Author

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.

@fana605
Copy link

fana605 commented Apr 2, 2021

Database Table:

site_id class property value
x App baseURL site1.com
x App defaultLocale kgp
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 $_SERVER var, the Host, etc.

Most important would be the "config" Event. Executed in the bootstrap file, before the current pre_system. (Or the pre_system could be moved?)

This would be perfect for my case. I need to set $supportedLocales from the database. I tried to do it on a filter, but obviously, it's too late in the bootstrap process because the App config is already loaded into the framework.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants