未加载调试模块时,Yii2登录不起作用

时间:2018-04-20 19:22:45

标签: yii yii2 yii-components yii-modules

我们有一个有效的Yii2应用程序,使用Yii1的旧版本作为基础进行了Frankensteinized,然后更新为Yii2。当然有很多问题,我设法解决了最多,但是有一个问题一直在躲避我。

只要我们将环境切换到DEV之外的其他任何内容,此代码就不再执行:

$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
    'class' => 'yii\debug\Module',
    // uncomment the following to add your IP if you are not connecting from localhost.
    //'allowedIPs' => ['127.0.0.1', '::1'],
];

如果不执行此操作,我们将无法再登录。我通过离开DEV环境并仅对该位进行评论来隔离该问题。我不知道为什么会话或身份似乎没有保存用户ID,只是不断显示登录页面。

我知道登录,UserIdentityInterface在Yii2上发生了很大变化,我的狡猾的用户组件/接口可能是其中一个原因,但我无法弄清楚问题而且想知道如果有人罢工“似曾相识”,并认识到为什么调试模块能够正常工作。

我通过init()方法添加此内容来暂时修复它:

class UserIdentity extends \yii\web\User implements \yii\web\IdentityInterface
{
    function init()
    {
        parent::init();
        $this->id = Yii::$app->session->get('id');
    }
}

这样,Yii::$app->user->id将始终包含$_SESSION值 。




根据要求,我的完整web.php配置代码:

$params = require __DIR__ . '/params.php';
$db = require __DIR__ . '/db.php';

$config = [
    'id' => 'basic',
    'name' => 'NAME WIL BE HERE',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'aliases' => [
        '@bower' => '@vendor/bower-asset',
        '@npm' => '@vendor/npm-asset',
    ],
    'layout' => 'column1',
    'components' => [
        'request' => [
            'class' => 'app\components\NTDI_HttpRequest',
            'cookieValidationKey' => 'XXXXXXXXXXXXXXXXXXXX',
        ],
        'session' => [
            'class' => 'yii\web\DbSession',
            'name'=>'sess',
            'timeout'=>3600,
            'db'=>'db',
            'sessionTable'=>'session',
        ],
        'user' => [
            'identityClass' => 'app\components\UserIdentity',
            'class' => 'app\components\UserIdentity',
            'loginUrl' => array('/site/login'),
            'returnUrl' => array('/site/index'),
            'enableAutoLogin' => false,
        ],
        'errorHandler'=>array(
            'errorAction'=>'site/error',
        ),
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => $db,
        'language' => 'fr_CA',
        'sourceLanguage' => 'en_US',
        'urlManager' => [
            'class' => 'yii\web\UrlManager',
            'enablePrettyUrl' => false,
            'showScriptName' => true,
            'rules' => [
                '' => 'site/index',
                'membre' => 'site/login',
                'nip' => 'member/nip',
                'verif' => 'member/verif',
                'vote' => 'member/vote',
                'merci' => 'member/logout',
            ],
        ],
        'i18n' => [
            'translations' => [
                '*' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'fileMap' => [
                        'general' => 'general.php',
                        'landing' => 'landing.php',
                        'vote' => 'vote.php',
                        'app/error' => 'error.php',
                    ],
                ],
            ],
        ],
    ],
    'params' => $params,
];


// #BUG# If this is not set, we can't login anymore and the system keeps asking for a username numnber.. why??
//*
if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = [
        'class' => 'yii\debug\Module',
        // uncomment the following to add your IP if you are not connecting from localhost.
        //'allowedIPs' => ['127.0.0.1', '::1'],
    ];
}
//*/

if (YII_ENV_DEV) {
    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
        // uncomment the following to add your IP if you are not connecting from localhost.
        'allowedIPs' => ['127.0.0.1', '::1', '192.222.216.153', '24.37.138.46'],
    ];
}

return $config;

根据@ rob006的要求,我的UserIdentity code。 (对不起,它太乱了,大部分代码已经存在,而且我不想太多改变

0 个答案:

没有答案