简体   繁体   English

我无法在yii2中保存用户身份

[英]I can't save user identity in yii2

I created a module in yii2 under api/modules/v1 folder, the problem is when I request: 我在yii2的 api / modules / v1文件夹下创建了一个模块,问题是当我请求时:

Yii::$app->user->login($user, 3600); 

The identity got saved temporary until I refresh the page or I request another url, When I reviewed the log file, I found this : User '1' logged in from ::1. 在刷新页面或请求其他URL之前,身份已临时保存,当我查看日志文件时,发现: 用户'1'从:: 1登录。 Session not enabled. 会话未启用。 Also I tried to add : 我也尝试添加:

'enableAutoLogin' => true,
'enableSession' => true,

user component configuration : 用户组件配置:

'user' => [
        'identityClass' => 'common\models\User',
        'enableAutoLogin' => true,
        'enableSession' => true,
        'identityCookie' => [
            'name' => '_APIUser', // unique for backend
            'path' => '/api/web/v1'  // correct path for the backend app.
        ]
    ],
    'session' => [
        'name' => '_apiSessionId', // unique for frontend
        'savePath' => __DIR__ . '/../runtime', // a temporary folder on frontend
    ],

But the same problem occurred. 但是发生了同样的问题。

Have you model common\\models\\User implements "login" method? 您是否已建模common \\ models \\ User实现“登录”方法? If so, login with this code 如果是这样,请使用此代码登录

Yii::$app->user->identity->login($user, 3600); 

it seems like you are configuring 'common\\config\\main' as you have indicated frontend and backend session details in the same config. 似乎您正在配置“ common \\ config \\ main”,因为您在同一配置中指示了前端和后端会话详细信息。

Leave the common\\config\\main blank and you have to configure your backend\\config\\main and frontend\\config\\main separately. 将common \\ config \\ main保留为空白,您必须分别配置backend \\ config \\ main和frontend \\ config \\ main。 Your login code should work. 您的登录代码应该可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM