0

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.

arogachev
  • 33,150
  • 7
  • 114
  • 117
Abhimanyu Saharan
  • 642
  • 1
  • 10
  • 26
  • What are you trying to accomplish by having a dynamic config file? If you want the same Yii application to use different configs for say different users, it's going to need a config to instantiate in the first place. – deacs Jan 25 '15 at 22:58
  • @deacs We're trying to make an auto-installer for our application wherein parameters that are hardcoded into the `main-local.php` and `params-local.php` should be changeable at runtime. – Abhimanyu Saharan Jan 26 '15 at 04:52

1 Answers1

1

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.

Community
  • 1
  • 1
Mihai P.
  • 9,307
  • 3
  • 38
  • 49
  • I was also thinking the same think to create my own config files. Further more, I have started working on a starter template(advanced) which will have the gui thingy to setup the application. I'm still stuck at how the application will come to know that it has started for the first time (means there is no database connection or the database doesn't exist at all). If it is the latter case, the application will then guide the user through the installation. So, basically I need a more robust way to deal with this. If you have any ideas, do share. Thank you. – Abhimanyu Saharan Jan 27 '15 at 07:17
  • I will build something similar in the near future. From what I gather I will need 1 config that is in a file, the database config. You can move this to a different file and test for the existence of this file to decide if this is the first time you start it. We are trying to make the entire install process automated (command line only) so we will not have such a install procedure. – Mihai P. Jan 27 '15 at 12:38