A
A
Alexey Alekseev2018-11-09 21:39:50
symfony
Alexey Alekseev, 2018-11-09 21:39:50

How to store user preferences in Symfony?

Hello.
Just starting to learn Symfony, a question arose.
I need to save/read the settings of my application.
After reading the documentation, I realized that in Symfony it is better to store settings in YML files.
So, I created a YML file (setting.yml), for example:

organization:
   office_1:
      set_1: "example_1"
      set_2: "example_2"
      ...
   office_2:
      set_1: "example_1"
      set_2: "example_2"
      ...

I read the settings without problems in this way:
I get an array:
$value = Yaml::parseFile($this->get('kernel')->getRootDir() . '/setting.yml');

Then I get the setting I need from the array.
There was a problem writing settings to a file. According to the documentation there is a method:
$yaml = Yaml::dump($array);
file_put_contents('/setting.yaml', $yaml);

But this method overwrites all the settings in the file again, which does not suit me.
Is it possible to somehow change the value of a certain element in the settings file by key without overwriting the entire file?
Thank you!
Or maybe there is some other way to store application settings? maybe xml?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Shumov, 2018-11-09
@inoise

No need for a file. Write to the database

V
Vadim, 2018-11-10
@kylt_lichnosti

The format doesn't really matter. Sympha supports yaml, xml, php.
Another question is what if these are real application settings and they are not dynamic, but permanent, like passwords, databases, etc. Then use the parameters:
https://symfony.com/doc/current/service_container/...
At the expense of overwriting the file - you read its contents, change what you need in the resulting array and save it with your method. I did. But this option is for rare work at the same time by only one person (for example, an administrator). If several people should work with this file at the same time, then, as written above, you must use the database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question