Web Development – Best Practice for Configuration Files

configurationconfiguration-managementweb-development

I am creating something similar to a Content Management System, that can be downloaded and used by everyone that needs it. I've been working in web-development for a couple of years (mainly as a hobby, i.e. creating personal homepages for acquaintances), but have never had the need of a user to enter his own configurations (let it be database connection, meta-data about his site, desired behaviour of the website).

Therefore I have no experience with custom configuration file handling. That's where my question lies: What is the best practice to handle custom website-wide configuration files and handle them in PHP?

I have thought of creating INI-files, that can be customized from within the admin panel and that are hidden to the user by .htaccess for security purposes. However I don't know if INI is the best method to do this considering that the page is being developed to be extensible through plugins (which may lead to another question soon).

So what is my best bet to handle configurability? For instance do I want to store database configuration in files somehow and have other configurations outsourced to the database itself? Or do I store everything in INI-files (also considering configurability for plugins: Should everyone of these have their own config-file)?

Best Answer

Have you looked at other CMS and how they approach the problem?

For example, Drupal configuration responds to all your current needs and shows one possible way to not being limited by the constraints of INI.

You may also consider:

  • Ordinary PHP files (pros: simple, no parser needed; cons: the end user can screw the file and break the entire app),
  • INI (pros: still simple; cons: the flat structure is limiting),
  • XML (pros: no limits of INI; cons: not user-friendly, too much markup),
  • JSON (pros: less markup compared to XML; cons: still not user-friendly),
  • YAML (pros: humanly-readable; cons: the end-user can still screw the structure),
  • Custom page which allows the user to configure every aspect of the application though web interface (pros: user-friendly; cons: the excessive cost of developing the solution).

If you're working with end-users who don't have technical background, the last solution is the only one viable.

Applications like PHPBB show how to make two-step configuration: when the application runs for the first time, it executes setup.php which asks for the connection string, the root password, etc. Once the app is installed, the administrator can log in to access the administration panel.

Related Topic