简体   繁体   English

获取yii2中的用户名

[英]Get the name of the user in yii2

How can I get the name of the logged-in-user in yii2?如何在 yii2 中获取登录用户的名称? I can get the user-id with我可以用

Yii::$app->user->id;

and I know that I could find the name in the database but I want a direct way.我知道我可以在数据库中找到这个名字,但我想要一个直接的方法。 The name-column in the database has the name "username", but数据库中的名称列具有名称“用户名”,但

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

doesn't work and不起作用并且

Yii::$app->user->name;     

doesn't work either.也不起作用。

On login the user information will be stored in Yii::$app->user->identity variable.登录时,用户信息将存储在Yii::$app->user->identity变量中。

For more information have a read through the User Authentication documentation in the official guide.有关更多信息,请阅读官方指南中的用户身份验证文档

While the answer from @thepeach works, you can actually extend the User component and add your own functions, so that you can get them via Yii::$app->user->something as you were initially trying to do.虽然@thepeach 的答案有效,但您实际上可以扩展 User 组件并添加您自己的函数,以便您可以通过Yii::$app->user->something获取它们,就像您最初尝试做的那样。

I like to extend things like this from the start, so I am ready to add custom functionality without having to refactor any code.我喜欢从一开始就扩展这样的东西,所以我准备添加自定义功能而无需重构任何代码。 It sucks to do things one way, then have to go back and fix 100 spots of code, because you changed it later.以一种方式做事很糟糕,然后不得不回去修复 100 个代码点,因为你后来改变了它。

First, define a user component class in your config:首先,在您的配置中定义一个用户组件类:

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

Then create User.php in your components directory.然后在您的components目录中创建User.php If you haven't made this directory, create it in your app root.如果您尚未创建此目录,请在您的应用根目录中创建它。

User.php用户名

<?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;
    }
}

Now you can access these through Yii::$app->user->something .现在你可以通过Yii::$app->user->something访问这些。

For example, put this in one of your views and access the page in your browser:例如,将其放在您的一个视图中并在浏览器中访问该页面:

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

I wrote a more detailed answer here , which covers this a bit more in depth.在这里写了一个更详细的答案,它更深入地介绍了这一点。

简单,只需使用:

<?= \Yii::$app->user->identity->username ?>
$user_id = Yii::$app->user->id;
$user_name = User::find()->where(['id'=>$user_id])->one()->username;

I always prefer below code to find current user.我总是更喜欢下面的代码来查找当前用户。 use only this and you will get the identity.只使用这个,你就会得到身份。

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

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

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