M
M
Mark2019-03-06 18:39:14
Yii
Mark, 2019-03-06 18:39:14

How to store settings in Yii2?

There are several groups of settings that need to be edited.

Parameter example

-- Partner
- percent_deduction
- minimum_withdraw
-- Yandex_Money
- bill
- secret_key

How can you correctly implement editing / using these settings?
So far I see this option:
1. Create a settings table , with fields:
- module - settings group name
- data - settings parameters in JSON
2. Create a model on which to hang the behavior of converting data to JSON on beforeSave , and converting from JSON to an object on afterFind()
3. Editing will be carried out in a separate form created for customization.
4. Loading parameters will be hung on bootstrap, which will receive all the settings and write them to params, in a cell with the name of the name of the settings group. That is: Yii::$app->params['partner']['percent_deduction']. You can also add a cache, which will be reset when the settings change.
But I don't like this method because of:
1. Magic in forms. In fact, only the form knows the real names of the settings. Something like:
Screenshot example
Hv0qqQl.jpg

2. The inconvenience of using parameters, again, it is very easy to make a mistake and constantly have to peek at the settings in the form.
Another option that comes to mind is to create separate settings classes and create a behavior based on the Symfony Serializer, but so far I haven’t figured out what’s what and decided to ask, because perhaps there are already competent options that you can look at.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
grinat, 2019-03-06
@grinat

https://github.com/yii2mod/yii2-settings
but you can make your own, then it's better to make the configs come from a json or yml file, then the admin panel will not be needed.

K
KakorinCom, 2016-10-27
@kakorincom

What if the line is, for example:
Is it possible for it to find all occurrences? In my version, it finds everything, only doubles are parsed incorrectly, I don’t understand how to do it.
DECISION:
THANKS TO ALL.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question