Sometimes I would prefer to change and save the yii config (i.e. into the Db). If you look at many webapps, you usually have a file containing the Db config, then settings etc. are saved to the Db. I think that it would be nice to add the opportunity to save the yii config in the Db (only the Db config - db server, user, pass etc. - would be stored into a file).
There are indeed scenarios where you want the user to be able to edit some of the settings via a web interface. In my opinion it’s better to stick with the current approach though because it’s much faster and more flexible than loading stuff from the db. A different approach is to allow the admin backend to edit certain application config files. I’ve had some success with this approach in the past, you can just effectively do:
Although this does scrub any comments in the config file.
The real problem with either approach is building the UI. You end up needing a separate form model for literally every component that you want the user to be able to configure, as well as form models for any components which that editable component users. So for example, if you want to make the URL manager configurable you need a model for the url manager itself, a model for url rules and a UI that ties it all together. I’ve actually done this, it’s thousands of lines of code just for 2 components. Add the db, cache, session handler, auth manager, asset manager, client script etc, not to mention custom components, and you end up with a lot of code to write and maintain.
But the biggest problem of all - making this UI intuitive to end users. It’s all very well allowing them to configure e.g. $defaultParams on an URL rule but how do you explain that to them in a way non developers can understand? and can you do that for every configurable property of every configurable component?
Couldn’t you just serialize/unserialize or json_encode/json_decode arrays into/from the database? So instead of having a file with arrays, you just store those arrays into the db and you can also cache them. You keep the current approach but you add the capability to store the config data into the db for an easier management. There could be a gii component to manage the values.
This is the hard bit. The other hard bit is deciding which things can be configured, because obviously if you’re loading the config from the db, the db itself cannot be configurable, nor can the cache since presumably you’ll be caching these values.
what advantage does storing the config in the db really offer though? as i mentioned before, it’s trivial to parse and write the existing config files, and this is a LOT simpler than loading from the db etc.
In my opinion it’s simpler to query the Db instead of having to parse and write back a text file. Moreover you avoid issues like filesystem permission issues. I think my opinion is pretty popular as virtually any popular webapp stores the config data into the Db. Is there any popular webapp which parses and writes the config data into a text file? Writing a parser doesn’t sound very immediate to me, could you provide some code?
How do you use that $config var? Yeah we can just import the config file but we have to read the config values every time and overwrite the config file everytime and if you add config values you have to change the reading function to read the new values… haven’t we?
You use it just the same way you do now, in your index.php, it is the exact same method Yii currently uses to load its config, I’m just saying you can write files in this format too with 1 line of code. You don’t need to change your read / write functions when config changes, it’s just literally:
Read the config into an array using 1 line of code
Manipulate the array to your liking via a UI - THIS IS THE HARD BIT
? ? ?
Write the array back to the file using 1 line of code
It’s very simple. But again, you don’t respond about the UI difficulties.