Levi Hackwith
Levi Hackwith

Reputation: 9332

Should I use YAML for Config Files in a PHP Application?

I'm currently writing my own PHP Framework (for kicks, not for mission critical stuff) and I'm trying to add in functionality where the user can set up what databases the framework should use (a primary db and then maybe one or two fallbacks - like sqlite), where certain files are located, etc. Should I use YAML for this? Is there a better approach or a standard practice?

My Thoughts

  1. YAML is user (non-technical) friendly in terms of readability
  2. In order to keep my Framework from requiring non-standard PHP libraries, I'd have to use something like Symfony YAML in order to parse the file.
  3. Isn't Symfony moving away from YAML?
  4. I could use a PHP file full of variables but that would make setup of the framework less transparent to the user.

Update

I'm cleaning this question up to make it more constructive and to incorporate some of the answers I've gotten.

Overall Questions I Have

  1. What are the advantages of YAML over other approaches like XML or even an INI file setup?
  2. What is a good rule of thumb regarding when to use YAML over those other approaches or vice versa?

Upvotes: 20

Views: 12491

Answers (6)

ZJR
ZJR

Reputation: 9582

NO

Personal experience. YAML seems a wonderful idea and I loved it and its simplicity. Then I started investing time on it: the very same concept of being able to read it in a language and write in another was very enticing, but... cutting it short, it turned up to be a mere illusion, unsubstantiated by facts.

Every implementation of YAML differs too much from the other ones.

  • Arrays, automatically serialized by one, cannot sometimes be read by another.

  • Cross references are supported, but their implementation is really sketchy.

    References are powerful, but:

    • They are quite limited for some hardcore application.
    • They represent an overkill to most low-end YAML-based projects.


    So, frequently, they are ignored, and errored upon, by most parsers.

Summing it up, the standard isn't well set.

There are core concepts that are nice and simple, but the actual standard document is full of details about features that most people don't want to use and are difficult and expensive to implement.

There isn't a distinction of levels of compatibility, like there is in DOM (DOM level 1, DOM level 2, etc) so every parser implementor implements what they feel like to, to the extent they can bear, then they drop it and it's hard to discern what works and what doesn't.

Use Alternatives

  • JSON if you value the cross language data exchange language and little redundancy aspects as the top priority

  • INI if you value performance and backward compatibility (on PHP, as parse_ini_file() is fast, and there since... always) and readability/editability by humans, instead.

Upvotes: 32

stren-12
stren-12

Reputation: 57

use json good idea
for load config file

"username" : "root" //in json file 

 $json = file_get_contents('path/to/file');
 $data = json_decode($json);
 $data->username; //print root

for write config file

$data['username'] = 'root'; 

if (file_put_contents('path/to/file', json_encode($dat))) { echo "<h4 class='alert alert-success'>config updated</h4>"; }

last thing if you in web root use this .htaccess

<IfModule authz_core_module>
Require all denied
</IfModule>
<IfModule !authz_core_module>
Deny from all
</IfModule>

Upvotes: 1

bumperbox
bumperbox

Reputation: 10214

My personal preference is a for a PHP based configuration file.

i know php so to me learning yaml just for the config files is extra work when you could have a simple config file like this, which in essence is no harder them yaml, and doesn't require a special interpreter library, just include('config.php') and you are away

$config = array(
  'database' => array(
      'default' => array(
         'name' => 'dbname',
         'host' => 'localhost',
         'user' => 'username',
         'pass' => 'password'
      )
   )
);

then you can reference config settings like this

$host = $config['database']['default']['host'];

next step is to keep the config file simple, store the minimal amount of config data required, and then use the database to store the rest, and have admin screens for endusers to change settings within your application.

Upvotes: 7

Himanshu Patel
Himanshu Patel

Reputation: 172

I have dig bit more for config file format and found interesting facts for YAML format.

Mainly it have few drawback

1) It involve installation and configuration of addition PHP library as YAML Module is not coming by default or have to decouple from symphony framework and use it.

2) Read and Write performance is worst among all config technologies, if compare to INI, XML, JSON . Taking lots more time read compare to INI file http://konrness.com/php5/zend_config-benchmark-json-array-ini-xml-yaml/

3) Readability is not good, compare to INI and XML format. When it became large then difficult to read and manage with humane eyes.

4) Bit bulky compare to INI and JSON.

So it’s better to use INI format rather than YAML.

Upvotes: 2

xaav
xaav

Reputation: 7916

If you're writing a framework, then yes. You will have to end up doing more work on your part, but the goal of a framework is to make things easier for the person developing the application.

Isn't Symfony moving away from YAML?

No, Symony2 is almost entirely configured by YAML.

Upvotes: 1

Daniel
Daniel

Reputation: 31579

What I usually do is make an XML file and make a non dependent frontend to modifying the settings in the XML file.

Upvotes: 0

Related Questions