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

问题描述:

如何使用Yii::$app->user->something添加或获取变量?

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

我不想让他们通过identity.我想通过user来访问特定的对象.

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

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

虽然本文中的其他主题提供了一种可靠的黑客解决方案,或者只是通过Yii::$app->user->identity来获取它,但我将向您展示如何正确地做到这一点. .您可以通过虚拟扩展所有内容来随心所欲地改变Yii2!

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 app\path\to.这是文件所在的位置.例如,您在应用程序根目录models中具有ContactForm.php,因此其名称空间为app\models.在高级中,您有多个应用程序(前端和后端),因此应用程序根目录不同,因此,一个应用程序根目录为frontend,另一应用程序根目录为backend.高级应用程序中的models目录位于"frontend/models",其名称空间为frontend\models. -因此,在命名空间中看到"app"的任何地方,都可以用"frontend"或"backend"代替高级"模板.

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.

首先,我们需要修改应用程序的配置文件,以覆盖默认的User组件的类.我将忽略任何不相关的内容,因此您必须定义您的identityClass,我只是不显示它.我们只是在添加而不是替换配置.另外,在显示frontend/something的地方,也可以对backend/something执行相同的操作.我只是不想一遍又一遍地重复...

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...

基本(config/web.php)-或-高级(frontend/config/main.php)

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

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

记住:如果您使用的是高级模板,请将app替换为frontendbackend

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

我还想指出,如果您尚未创建"components"目录,则需要这样做.这是许多人添加自定义类的地方.无论是您自己的帮助器类,还是扩展基本Yii组件(例如扩展yii\web\Controller).

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).

在您的components目录中创建一个名为User.php的新文件.在该文件中,放置以下代码:

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

注意:usernamename在数据库的用户表中必须是有效的列名!

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

现在我们可以在控制器的任何位置使用它了.让我们对其进行测试.在views/site/index.php中,将其添加到页面底部:

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 :)