Yii2 - 触发后从事件处理程序终止App

时间:2017-02-27 10:52:22

标签: php yii2 yii2-user

我使用yii2-user进行用户注册。我想在从社交帐户登录之前检查用户是否已注册。我在创建帐户并在main.php全球范围内处理帐户之前触发事件。

如果用户已经注册,我想显示一些消息,不要让他继续注册/登录过程。

简而言之,我希望用户被重定向到主页,如果指定的条件匹配,则显示来自事件处理程序的消息。

'modules' => [
    'user' => [
        'class' => 'dektrium\user\Module',
        'enableRegistration' => true,
        'controllerMap' => [
            'security' => [
                'class' => \dektrium\user\controllers\SecurityController::className(),
                'on ' . \dektrium\user\controllers\SecurityController::EVENT_BEFORE_CREATE_ACCOUNT => function ($e) {

                    $email = $e->client->email;

                    $user_model = \dektrium\user\models\User::findOne(['email' => $email]);

                    if($user_model == Null)
                    {
                        return;     //then just return and continue next steps
                    }
                    Yii::$app->session->setFlash('info', "You cannot directly login with this account. Please login with the credentials sent to your email ($email) first and then connect your Account from Settings.");
                    //---------- NOW REDIRECT TO HOME PAGE--------
                }
            ],
        ],
    ],
],

将用户重定向到主页并停止执行当前控制器的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

添加:

//---------- NOW REDIRECT TO HOME PAGE--------
Yii::$app->response->redirect(Url::home());
Yii::$app->end();