简体   繁体   English

如何获取登录的用户ID

[英]How to get the logged in user id

sorry for not specifying question properly as i dont even know what should i write on there but here is my problem. 很抱歉没有正确地指定问题,因为我什至不知道该在那写什么,但这是我的问题。

I have 2 tables users and organiser and they both are using same desktop system for different privileges on my project and have 2 different login pages. 我有2个表的usersorganiser ,他们都使用同一桌面系统来获得我项目的不同权限,并且具有2个不同的login页面。

Now the confusion is that I want to introduce another feature to list the advertisement on website and i want both of the users to post ad on website. 现在的困惑是,我想引入另一个功能以在网站上列出广告,并且我希望两个用户都可以在网站上发布广告。 For that sometimes i need to get the id and other details of the logged in user,so what should i do here to know which user is logged in or organiser is logged in? 为此,有时我需要获取已登录用户的id和其他详细信息,那么在这里我应该怎么做才能知道哪个user已登录或organiser已登录?

Should i make any changes in database? 我应该在数据库中进行任何更改吗? or add any extra field in the advertisement table which indicates which type of user has created that record??? 或在advertisement表中添加任何额外字段,以指示哪种类型的用户创建了该记录??? I am using yii2 framework if that matters. 如果重要的话,我正在使用yii2框架。

Here is the code of frontend configuration file in yii2 这是yii2frontend配置文件的yii2

return [
'id' => 'app-frontend',
'controllerNamespace' => 'frontend\controllers',
'defaultRoute' => 'event/index',
'components' => [
    'user' => [
        'identityClass' => 'common\models\User',
        'enableAutoLogin' => true,
        'identityCookie' => [
            'name' => '_frontendUser', // unique for frontend
        ],
    ],


    'session' => [
        'name' => 'PHPFRONTSESSID',
        'savePath' => sys_get_temp_dir(),
    ],
    'request' => [
        // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
        'cookieValidationKey' => 'YNdaEYHtai',
        'csrfParam' => '_frontendCSRF',
    ],

   ],
 ];

For adding field or method to User class you can or extend the User class ( User doc ) , or create an your new User class implementing the identity interface ( Identity doc ) . 要向User类添加字段或方法,您可以扩展User类( User doc ),或创建实现身份接口( Identity doc )的新User类。

depend by your experience or ability. 取决于您的经验或能力。

(You can also evaluate extensions related to User management) (您也可以评估与用户管理相关的扩展名)

For accessing the current logged user you can use 要访问当前登录的用户,您可以使用

for username 用户名

Yii::$app->user->identity->username

for id 为id

Yii::$app->user->id

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

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