Home arrow PHP arrow Page 9 - Easy Application Configuration With patConfiguration

When Time Is Money, Recycle! - PHP

Tired of handcrafting configuration file manipulation tools for your Web application? Save yourself some time with patConfiguration, a PHP class designed to assist developers with reading, writing and maintaining application configuration files.

  1. Easy Application Configuration With patConfiguration
  2. Plug And Play
  3. Your Friendly Neighbourhood Spiderman
  4. Anatomy Class
  5. Version Control
  6. The Write Stuff
  7. Speaking Native
  8. Not Your Type
  9. When Time Is Money, Recycle!
  10. Cache Cow
  11. Link Zone
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 1
January 29, 2003

print this article


Here's another example, this one demonstrating how patConfiguration can be used in the context of a script accepting user input for application configuration. This script is divided into two parts: a form which displays the current configuration (if available) and allows the user to edit it, and a form processor, which accepts the new configuration and saves it to a file.

In addition to patConfiguration, this script also uses the patTemplate engine for the actual interface generation - you can read more about patTemplate at http://www.devshed.com/Server_Side/PHP/patTemplate/

Here's the template,

<patTemplate:tmpl name="form"> <html> <head><basefont face="Arial"></head> <body> <h2>Configuration</h2> <table border="0" cellspacing="5" cellpadding="5"> <form action="configure.php" method="post"> <tr> <td>MySQL host name</td> <td><input type="text" name="db_host" value="{DB_HOST}"></td> </tr> <tr> <td>MySQL user name</td> <td><input type="text" name="db_user" value="{DB_USER}"></td> </tr> <tr> <td>MySQL user password</td> <td><input type="text" name="db_pass" value="{DB_PASS}"></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" name="submit" value="Save Configuration"></td> </tr> </form> </table> </body> </html> </patTemplate:tmpl>
and here's the script that does all the work:

<? // include classes require("patConfiguration.php"); require("patTemplate.php"); // create patConfiguration object $conf = new patConfiguration; // set config file locations $conf->setConfigDir("config"); // create patTemplate object $template = new patTemplate; // set template location $template->setBasedir("templates"); // add templates to the template engine $template->readTemplatesFromFile("configuration.tmpl"); // form not yet submitted if (!$_POST["submit"]) { // check to see if config file exists if (file_exists("config/config.xml")) { // parse it and display configuration values $conf->parseConfigFile("config.xml"); $template->AddVar("form", "DB_HOST", $conf->getConfigValue("db.host")); $template->AddVar("form", "DB_USER", $conf->getConfigValue("db.user")); $template->AddVar("form", "DB_PASS", $conf->getConfigValue("db.pass")); } // parse and display the template $template->displayParsedTemplate("form"); } else { // accept the submitted values // and write them to a configuration file $conf->setConfigValue("db.host", $_POST['db_host']); $conf->setConfigValue("db.user", $_POST['db_user']); $conf->setConfigValue("db.pass", $_POST['db_pass']); $conf->writeConfigFile("config.xml", "xml", array("mode" => "pretty")); } ?>
In this case, patConfiguration is used to read the application's database configuration from a file via the parseConfigFile() method and display the variable-value pairs contained within that file in an editable HTML form. The user may then modify these values and submit the form; patConfiguration will accept the new values and write them back to the configuration file via writeConfigFile().

This kind of application configuration is pretty common to most Web-based tools - and patConfiguration lets you build an interface around it quickly and efficiently, with maximum code reuse and minimal time wastage.

>>> More PHP Articles          >>> More By Vikram Vaswani, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: