Symfony2 Components – Yaml

Bookmark and Share

Yaml是一种标记语言,在Symfony中广泛用作配置文件。

使用Yaml组件解析一个简单的配置文件:
test.yml

database:
  driver
: pdo_mysql
  host
: localhost
  name
: testdb
  user
: root
  password
: 123456

解析

<?php
include '/www/lib/symfony/src/Symfony/Component/ClassLoader/UniversalClassLoader.php';
$loader=new \Symfony\Component\ClassLoader\UniversalClassLoader();
$loader->registerNamespaces(array(
    'Symfony'=>'/www/lib/symfony/src',
));

$loader->register();

use \Symfony\Component\Yaml\Yaml;
$data=Yaml::parse(__DIR__.'/test.yml');
print_r($data);

输出结果

Array
(
    [database] => Array
        (
            [driver] => pdo_mysql
            [host] => localhost
            [name] => testdb
            [user] => root
            [password] => 123456
        )

)
This entry was posted in Symfony2 and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>