K
K
Kirill Arutyunov2016-07-20 09:20:47
Yii
Kirill Arutyunov, 2016-07-20 09:20:47

How to use $app->params in models?

In the " Best Practices " section of the official guide, it is written that you should not directly access the session, requests, and environment variables in models.
Yii::$app->paramsjust contains an array of settings / parameters that are related to the environment and sometimes it is very convenient to store various global settings that can be used in the model. For example, apiUrl, some paths for uploading images, etc. And this data needs access directly from the model.
In general, there is no problem, but from the point of view of a competent approach to application design, this is incorrect (in accordance with the official guide).
How to pass these parameters to the model? When initializing the model in the constructor, throw everything? After all, this way you can skip / forget something, or even forget to pass any data to the constructor in some place.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Michael, 2016-07-21
@springimport

Probably, it is worth making a configuration class that will be passed to the model.
We still need to think about compliance with SOLID .

V
Valery, 2016-07-20
@supervaleha

And if you create a helper and work with it? Well, for example .. What will it look like from the point of view of architecture?

N
Nikita, 2016-07-20
@bitver

That's right, data enters the model only through the controller
"In order not to forget" - in the right place in the model, it is worth checking the property for data and otherwise throwing out InvalidConfig

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question