我需要创建类似"配置页面"的内容。用户可以使用配置表单编辑其设置。配置应该很容易从代码中获取。配置应支持不同类型的设置(标量,数组)
问题是:如何存储此设置? 现在我有两个想法:
感谢
答案 0 :(得分:0)
两者都很好。
这是一个带有parameters_user.yml
的例子use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Dumper;
public function indexAction(Request $request) {
$dir = $this->get('kernel')->getRootDir() . '/config/';
$filename = 'parameters_user.yml';
$path = $dir . $filename;
$yaml = new Parser();
try {
$values = $yaml->parse(file_get_contents($path));
} catch (ParseException $e) {
printf("Unable to parse the YAML string: %s", $e->getMessage());
}
$config = new Config();
$config->setConfig($values['parameters']);
$form = $this->createForm(new ConfigType, $config);
$form->handleRequest($request);
$this->processForm($form, $path);
return array('form' => $form->createView());
}
private function processForm($form, $path) {
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$config = $form->getData();
$values['parameters'] = $config->toArray();
$dumper = new Dumper();
$yaml = $dumper->dump($values, 2);
file_put_contents($path, $yaml);
}
}