Reputation: 654
I want to create the config file dynamically as in runtime. I have a table named Settings
in a database. Its structure is like:
|Column |Type |Length|
|id |int |11 |
|key |varchar |100 |
|value |varchar |255 |
|created_at |datetime| |
|updated_at |datetime| |
Now when the application starts for the first time, it should check whether it is connected to a database or not. If not, then it should be redirected to the installer
page. Where the user will enter the necessary details and the config file will be generated accordingly and the same data will be stored in the Settings
table for future use.
I don't want to hardcode any values into the config file. So, how can I achieve this task? Any ideas?
I have thought of an idea. What if I check the params-local.php
file for a param called installed
with a value true
. If it exists, we say that the application has been installed successfully else we take the user to the installer
page.
Upvotes: 0
Views: 1172
Reputation: 9367
You can always use closures (anonymous functions) to get any kind of params you want to any place you desire.
See here: Multi Tenant Multiple Database Setup
What I would do is to use a caching server like memcached, make sure you cache the values after you read them from the DB, you really do not want the same queries to be run over and over again. Afterwards use a closure to merge the data that you have read and cached from the DB to any default values you have while still using the same config files.
If you really want to speed things along in your index.php file, when reading the config you can create your own config array any way you desire (I would still use memcached) and start the application with that config. The config files are basically just arrays that are used when starting the application.
1 thing, you probably cannot use any yii specific functions in either place because the Yii application will be available until after you start it (with a config I mean) so you might have to create the functions in PHP directly.
Upvotes: 1