P
P
Pavel Gogolinsky2015-10-13 12:45:40
Yii
Pavel Gogolinsky, 2015-10-13 12:45:40

Where is the right place to set a variable in a session in Yii?

There is a site with subdomains. You need to define a subdomain, find out the region and show information for the corresponding region. Where is it better to check the subdomain and set the value to the session?
Now I have made a separate BaseController class, I check and remember the values ​​​​of it in init (), I inherit from it in all frontend controllers. It works, but I don't like that I'm inheriting from a new class.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Makarov, 2015-10-13
@gogolinsky

Make a service if this data is needed in the future, otherwise the listener.
if component - sample config

return [
    'bootstrap' => [
        'mySubdomainComponent'
    ],
    'components' => [
        'mySubdomainComponent' => '\namespace\of\mySubdomainComponent'
    ]
];

// обработка данных в компоненте, метод init
...

// в дальнейшем можно получить компонент
\Yii::$app->get('mySubdomainComponent')

if listener - need to be added to bootstrap
$app = \Yii::$app;
$app->on($app::EVENT_BEFORE_REQUEST, ['myListener', 'method']);
// в листенере делаем нужную обработку
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question