2

In the advanced template, it shows how to "touch" the user record on successful login, but if the login is by cookie, this won't be called.

I have looked at Yii2 events, but am confused on how to include the parent.

Here is a tutuorial here on StackOverflow

And here is the source on GitHub

I know how to create my custom function, I just don't know how to call it from the afterLogin() method.

I don't really care if it is technically an event or not, I just need to override the native method and include the parent events.

Community
  • 1
  • 1
Mike Pearson
  • 505
  • 1
  • 5
  • 18
  • What do you mean with 'login by cookie'? – robsch Apr 21 '15 at 06:49
  • This is based on the [enableAutoLogin](https://github.com/yiisoft/yii2/blob/master/framework/web/User.php#L74) which is part of the identityInterface. And here is the [loginByCookie](https://github.com/yiisoft/yii2/blob/master/framework/web/User.php#L280) function. – Mike Pearson Apr 21 '15 at 12:17

2 Answers2

2

The following method is described in this github post.

Create a class the implements bootstrap interface, I did:

<?php
namespace app\bootstraps;
use yii\base\BootstrapInterface;

class AppBootstrap implements BootstrapInterface{

   public function bootstrap($app){
      $app->user->on(\yii\web\User::EVENT_BEFORE_LOGIN,['app\models\user\User', 'beforeLogin']);
      $app->user->on(\yii\web\User::EVENT_AFTER_LOGIN,['app\models\user\User', 'afterLogin']);
      $app->user->on(\yii\web\User::EVENT_BEFORE_LOGOUT,['app\models\user\User', 'beforeLogout']);
   }
}

I've overridden the user class and added the relevant functions there but you can reference any class and function you want.

Add the following to your web.php file

$config = [
    'id' => 'basic',
    'name' => 'x',
    'basePath' => dirname(__DIR__),
    'bootstrap' => [
        'log',
        'app\bootstraps\AppBootstrap',
    ],
.....etc
Harry B
  • 2,511
  • 1
  • 17
  • 40
1

you can put in /app/components/User.php with include parent

public function afterLogin($identity, $cookieBased, $duration){
        parent::afterLogin($identity, $cookieBased, $duration);
        // your code here, such set session
   }
jack
  • 401
  • 5
  • 10