Yii2 登录功能不起作用


Yii2 login Function not working

我已经按照 nenad/yii2-basic-template 的教程进行操作,应用程序似乎已经完美设置。除登录脚本之外的所有内容。我怀疑实际代码存在问题,但我的主机设置有问题。首次设置应用程序时,我创建了一个超级管理员用户没有问题,之后创建用户也不是问题,数据库中的所有内容都经过检查和更正。当我完成登录过程时,它似乎没有正确存储会话 VLU。如果我从站点控制器内的登录功能退出应用程序并返回 Yii::$app->user->身份,则输出是 id 期望看到的,但是如果我允许应用程序呈现视图并从视图中输出相同的变量,它将返回 null。

我正在运行 apache2、php 5.6、mysql 14.4 和其他不相关软件包的流浪盒子上运行该应用程序。 如果有人愿意,我可以上传任何代码或配置文件。 提前谢谢。

编辑

第一部分是_protected/控制器/siteController.php内部的登录功能,第二部分是从_Protected/views/site/index开始查看文件.php

/**
 * Logs in the user if his account is activated,
 * if not, displays appropriate message.
 *
 * @return string|'yii'web'Response
 */
public function actionLogin()
{
    if (!Yii::$app->user->isGuest) 
    {
        return $this->goHome();
    }
    // get setting value for 'Login With Email'
    $lwe = Yii::$app->params['lwe'];
    // if 'lwe' value is 'true' we instantiate LoginForm in 'lwe' scenario
    $model = $lwe ? new LoginForm(['scenario' => 'lwe']) : new LoginForm();
    // now we can try to log in the user
    if ($model->load(Yii::$app->request->post()) && $model->login()) 
    {
        var_dump(Yii::$app->user->identity);
        // var_dump($_SESSION);
        // if (!is_writable(session_save_path())) {
        //     echo 'Session path "'.session_save_path().'" is not writable for PHP!'; 
        // } else {
        //     echo 'Session Path "'.session_save_path().'" IS writable for PHP!';
        // }
        // phpinfo();
        exit;
        return $this->goBack();
    }
    // user couldn't be logged in, because he has not activated his account
    elseif($model->status === User::STATUS_NOT_ACTIVE)
    {
        // if his account is not activated, he will have to activate it first
        Yii::$app->session->setFlash('error', 
            Yii::t('app', 'You have to activate your account first. Please check your email.'));
        return $this->refresh();
    }    
    // account is activated, but some other errors have happened
    else
    {
        return $this->render('login', [
            'model' => $model,
        ]);
    }
}

<?php
/* @var $this yii'web'View */
$this->title = Yii::t('app', Yii::$app->name);
var_dump(Yii::$app->user->identity);
?>
<div class="site-index">
    <div class="jumbotron">
        <h1>Congratulations!</h1>
        <p class="lead">You have successfully installed Yii2 improved application template</p>
        <p><a class="btn btn-lg btn-success" href="http://www.freetuts.org/tutorial/view?id=6">Read our tutorial</a></p>
    </div>
    <div class="body-content">
        <div class="row">
            <div class="col-lg-3">

如您所见,我在登录函数中退出应用程序,VAR 转储中的数据是 id 期望看到的,但是如果我让应用程序转发到视图,则用户标识将返回为 null。

如果你在本地主机上使用 Yii2,那么默认情况下,STATUS_ACTIVE的值(在公共文件夹中可以找到的用户文件中)设置为 10。 例如:

const STATUS_ACTIVE = 10;

将此值更改为const STATUS_ACTIVE = 9;

在此之后,您将能够完美登录。

最简单的解决方案是更改数据库表中的状态值。