[英]How to get the logged in user id
很抱歉沒有正確地指定問題,因為我什至不知道該在那寫什么,但這是我的問題。
我有2個表的users
和organiser
,他們都使用同一桌面系統來獲得我項目的不同權限,並且具有2個不同的login
頁面。
現在的困惑是,我想引入另一個功能以在網站上列出廣告,並且我希望兩個用戶都可以在網站上發布廣告。 為此,有時我需要獲取已登錄用戶的id
和其他詳細信息,那么在這里我應該怎么做才能知道哪個user
已登錄或organiser
已登錄?
我應該在數據庫中進行任何更改嗎? 或在advertisement
表中添加任何額外字段,以指示哪種類型的用戶創建了該記錄??? 如果重要的話,我正在使用yii2
框架。
這是yii2
的frontend
配置文件的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',
],
],
];
要向User類添加字段或方法,您可以擴展User類( User doc ),或創建實現身份接口( Identity doc )的新User類。
取決於您的經驗或能力。
(您也可以評估與用戶管理相關的擴展名)
要訪問當前登錄的用戶,您可以使用
用戶名
Yii::$app->user->identity->username
為id
Yii::$app->user->id
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.