Is it possible to configure a Silex Application with YAML config files? I bet yes, but how is it done correctly? For instance I want to use different configurations according to the environment, like config.dev.yml and config.prod.yml.
是否可以使用YAML配置文件配置Silex应用程序?我打赌是的,但它是如何正确完成的?例如,我想根据环境使用不同的配置,如config.dev.yml和config.prod.yml。
The config file should contain parameters for the app itself and for the registered extensions/services.
配置文件应包含应用程序本身和已注册扩展/服务的参数。
Example:
例:
// Debug flag should be set in the config file
$app['debug'] = true;
// Extension options should also be set in config file
$app->register(new Silex\Extension\TwigExtension(), array(
'twig.path' => __DIR__.'/views',
'twig.class_path' => __DIR__.'/vendor/Twig/lib',
));
Do I have to parse the YAML file by myself and set the parameters accordingly or is there a more "magic" way to do this?
我是否必须自己解析YAML文件并相应地设置参数,还是有更“神奇”的方法来做到这一点?
3 个解决方案
#1
37
First of all, add the Symfony Yaml component to your composer.json
首先,将Symfony Yaml组件添加到composer.json中
"symfony/yaml": "2.1.*@dev",
Use the right version choosing directly from the packagist page: https://packagist.org/packages/symfony/yaml
使用直接从包装页面选择正确的版本:https://packagist.org/packages/symfony/yaml
Now, you can add the deralex YamlConfigProvider, a simple and useful Silex provider. Add it to your composer.json:
现在,您可以添加deralex YamlConfigProvider,一个简单而有用的Silex提供程序。将它添加到您的composer.json:
"deralex/yaml-config-service-provider": "1.0.x-dev"
Here the official github page: https://github.com/deralex/YamlConfigServiceProvider
这里是官方的github页面:https://github.com/deralex/YamlConfigServiceProvider
Here the packagist page: https://packagist.org/packages/deralex/yaml-config-service-provider
这里包装页面:https://packagist.org/packages/deralex/yaml-config-service-provider
UPDATE
UPDATE
Install the dependencies with ./composer.phar update
command and finally add these lines to your app file:
使用./composer.phar update命令安装依赖项,最后将这些行添加到您的app文件中:
$app = new Silex\Application();
$app->register(new DerAlex\Silex\YamlConfigServiceProvider(__DIR__ . '/settings.yml'));
Now, for example, you can do this:
现在,例如,你可以这样做:
settings.yml
settings.yml中
database:
driver: pdo_mysql
host: localhost
dbname: database_name
user: root
password: password
charset: utf8
index.php
的index.php
$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
'db.options' => $app['config']['database']
));
#2
3
This package in the answer does not work for Silex 2.0 that is why I've created package that works for Silex 2.0 and Symfony/Yaml 3.1. Maybe someone looking for this answer will find it useful
答案中的这个包对Silex 2.0不起作用,这就是为什么我创建了适用于Silex 2.0和Symfony / Yaml 3.1的包。也许有人在寻找这个答案会发现它很有用
https://packagist.org/packages/rpodwika/yaml-config-service-provider
https://packagist.org/packages/rpodwika/yaml-config-service-provider
to use run command
使用run命令
composer require rpodwika/yaml-config-service-provider
or add
或添加
"rpodwika/yaml-config-service-provider" : "dev-master"
to your composer.json
到你的composer.json
github link https://github.com/rpodwika/yaml-config-service-provider
github链接https://github.com/rpodwika/yaml-config-service-provider
to use:
使用:
<?php
require_once __DIR__.'/../vendor/autoload.php';
$app = new Silex\Application();
$app->register(new Rpodwika\Silex\YamlConfigServiceProvider("settings.yml"));
echo $app['config']['database']['driver'];
#3
0
The LoadConfigExtension described by @fbrandel (above in comments) allows you to share the yml loader config service.
@fbrandel描述的LoadConfigExtension(在注释中)允许您共享yml loader配置服务。
#1
37
First of all, add the Symfony Yaml component to your composer.json
首先,将Symfony Yaml组件添加到composer.json中
"symfony/yaml": "2.1.*@dev",
Use the right version choosing directly from the packagist page: https://packagist.org/packages/symfony/yaml
使用直接从包装页面选择正确的版本:https://packagist.org/packages/symfony/yaml
Now, you can add the deralex YamlConfigProvider, a simple and useful Silex provider. Add it to your composer.json:
现在,您可以添加deralex YamlConfigProvider,一个简单而有用的Silex提供程序。将它添加到您的composer.json:
"deralex/yaml-config-service-provider": "1.0.x-dev"
Here the official github page: https://github.com/deralex/YamlConfigServiceProvider
这里是官方的github页面:https://github.com/deralex/YamlConfigServiceProvider
Here the packagist page: https://packagist.org/packages/deralex/yaml-config-service-provider
这里包装页面:https://packagist.org/packages/deralex/yaml-config-service-provider
UPDATE
UPDATE
Install the dependencies with ./composer.phar update
command and finally add these lines to your app file:
使用./composer.phar update命令安装依赖项,最后将这些行添加到您的app文件中:
$app = new Silex\Application();
$app->register(new DerAlex\Silex\YamlConfigServiceProvider(__DIR__ . '/settings.yml'));
Now, for example, you can do this:
现在,例如,你可以这样做:
settings.yml
settings.yml中
database:
driver: pdo_mysql
host: localhost
dbname: database_name
user: root
password: password
charset: utf8
index.php
的index.php
$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
'db.options' => $app['config']['database']
));
#2
3
This package in the answer does not work for Silex 2.0 that is why I've created package that works for Silex 2.0 and Symfony/Yaml 3.1. Maybe someone looking for this answer will find it useful
答案中的这个包对Silex 2.0不起作用,这就是为什么我创建了适用于Silex 2.0和Symfony / Yaml 3.1的包。也许有人在寻找这个答案会发现它很有用
https://packagist.org/packages/rpodwika/yaml-config-service-provider
https://packagist.org/packages/rpodwika/yaml-config-service-provider
to use run command
使用run命令
composer require rpodwika/yaml-config-service-provider
or add
或添加
"rpodwika/yaml-config-service-provider" : "dev-master"
to your composer.json
到你的composer.json
github link https://github.com/rpodwika/yaml-config-service-provider
github链接https://github.com/rpodwika/yaml-config-service-provider
to use:
使用:
<?php
require_once __DIR__.'/../vendor/autoload.php';
$app = new Silex\Application();
$app->register(new Rpodwika\Silex\YamlConfigServiceProvider("settings.yml"));
echo $app['config']['database']['driver'];
#3
0
The LoadConfigExtension described by @fbrandel (above in comments) allows you to share the yml loader config service.
@fbrandel描述的LoadConfigExtension(在注释中)允许您共享yml loader配置服务。