0

I am using yii2-basic. I want to use active sessions everywhere in yii2. How can I do it? I mean where should I put this code (layout.php? web\index.php?) or there is any config in config.php to make session auto active?

$session = Yii::$app->session;

Sorry for my bad english.

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
user1571234
  • 261
  • 3
  • 4
  • 9
  • I don't think you need this. As soon as you use `Yii::$app->session` it gets opened automatically. – robsch Jun 22 '15 at 14:09
  • @robsch yes, i know, but i want to active session as soon as they access to my website, so where should i put this code in ? – user1571234 Jun 22 '15 at 14:30
  • How early should it be? What do you need that for? I guess there is probably a better solution. You could create a component and load it in the [boostrap](http://www.yiiframework.com/doc-2.0/guide-structure-applications.html#bootstrap) process. – robsch Jun 22 '15 at 14:43
  • I created my own login user function (not yii2's rbac) and i want to check users' session at every page of my site. So what should i do ? @robsch – user1571234 Jun 22 '15 at 15:30

1 Answers1

0

In your web.php config file file you could add an event handler:

$config = [
    ...
    'on beforeAction' => function ($event) {
        // check Yii::$app->session
    },
    ...
];

There you can set $event->isValid to false what would mean that the action won't get executed. This gets called on any action. See also this.

Or create a component that you load on bootstraping like it is described here.

Community
  • 1
  • 1
robsch
  • 9,358
  • 9
  • 63
  • 104