dqx24298 2016-07-16 20:34
浏览 1865
已采纳

Yii2 - 如何从Yii :: $ app-> user获取当前用户名或名称?

How do you add, or get, variables using Yii::$app->user->something?

I don't want to get them through identity. I want to make specific ones accessible through user.

For example, I want to get the current logged in user's username and name:

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

or

\Yii::$app->user->name
  • 写回答

1条回答 默认 最新

  • donkey111111 2016-07-16 20:34
    关注

    While other topics here on SO offer a chincy hack solution, or to simply fetch it through Yii::$app->user->identity, I am going to show you how to do it properly. You can bend Yii2 to your will, by virtually extending everything!

    I normally work in the "advanced" app, but I am going to just use the "basic" app as a reference for everyone. I assume those who are using the "advanced" app know the differences and understand namespaces.

    Namespace Crash Course: In basic, you have namespace app\path\to. This is where the file is located. For example, you have ContactForm.php located in the app root models directory, so it's namespace is app\models. In advanced, you have multiple apps (frontend & backend), so the app root is different, thus you have frontend as one app root, and backend as another. The models directory in the advanced app is at "frontend/models" and it's namespace is frontend\models. -- So anywhere you see "app" in the namespace, you can replace it with frontend or backend for the "advanced" template.


    Solution

    First, we need to modify our app's config file to override the default User component's class. I will leave out anything not related, so you keep your identityClass defined, I just am not showing it. We are simply adding to, not replacing, the config. Also, where I show frontend/something, the same can be done for backend/something. I just am not going to repeat both over and over again...

    Basic (config/web.php) -or- Advanced (frontend/config/main.php)

    'components' => [
        'user' => [
            'class' => 'app\components\User', // extend User component
        ],
    ],
    

    Remember: If you are using the advanced template, replace app with frontend or backend!

    I also want to point out, that you will need to create the "components" directory if you haven't done so. This is where many people add their custom Classes. Whether it be your own helper class, or extending a base Yii component (such as extending yii\web\Controller).

    Create a new file in your components directory named User.php. In that file, place the following code:

    <?php
    namespace app\components;
    
    use Yii;
    
    /**
     * Extended yii\web\User
     *
     * This allows us to do "Yii::$app->user->something" by adding getters
     * like "public function getSomething()"
     *
     * So we can use variables and functions directly in `Yii::$app->user`
     */
    class User extends \yii\web\User
    {
        public function getUsername()
        {
            return \Yii::$app->user->identity->username;
        }
    
        public function getName()
        {
            return \Yii::$app->user->identity->name;
        }
    }
    

    Note: username and name must be valid column names in your database's user table!

    So now we can use this in our controller, view, anywhere.. Let's test it out. In views/site/index.php, add this to the bottom of the page:

    <?= \Yii::$app->user->name ?>
    <?= \Yii::$app->user->username ?>
    

    If all is good, and you have values in the user table for name and username, they should be printed on to the page :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵