A
A
AlexSer2021-04-08 10:22:43
Yii
AlexSer, 2021-04-08 10:22:43

Is it possible to declare parameters in yii2 so that all controllers receive it?

How to declare parameters so that all controllers receive them. The fact is that I have several organizations in the project,
and each time I write

User::findOne(Yii::$app->user->id)->organization->id

when updating and saving it is inconvenient. Is there any other way to solve it?
Yes, you can use sessions, but I would like it through the general parameters.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Denis, 2021-04-08
@sidni

Dumb idea overall no need for this functionality.
Well, you take Yuiya's base controller, inherit from it, get your base controller, override the constructor and write this line in it and save the result in a protected field (or in a private one with a getter).
You inherit the rest of your controllers from your received

Z
zerg2000, 2021-04-08
@zerg2000

Maybe Yii::$app->user->identity->organization->id ?

V
vitaly_74, 2021-04-08
@vitaly_74

first option: Yii::$app->user->identity->organization->id
Add to the User class (which is ActiveRecord) an association with hasOne (or hasMany) and associate it with the organization table
As far as I understand, you have already implemented this, simply try the
second option.
Add as a global variable i.e. Yii::$app->organization->id Third option
explained here : add as parameter Yii::$app->params['organization_id']; to do this, in the application config, add the column

'params' => [
        'organization_id' => User::findOne(Yii::$app->user->id)->organization->id,
    ],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question