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

2022-01-04 00:00:00 php yii2 yii yii2-user

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

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

比如我想获取当前登录用户的用户名和名称:

Yii::$app->user->username

Yii::$app->user->name

解决方案

虽然 SO 上的其他主题提供了一个 chincy hack 解决方案,或者只是通过 Yii::$app->user-> 获取它身份,我将向您展示如何正确地做到这一点.您可以通过几乎扩展所有内容来使 Yii2 随心所欲!

我通常在高级"应用程序中工作,但我将只使用基本"应用程序作为每个人的参考.我假设那些使用高级"应用程序的人知道差异并了解命名空间.

命名空间速成课程:基本上,您有命名空间 apppath o.这是文件所在的位置.例如,您的 ContactForm.php 位于应用根目录 models 中,因此它的命名空间是 appmodels.在进阶中,您有多个应用程序(前端和后端),因此应用程序根目录不同,因此您将 frontend 作为一个应用程序根目录,将 backend 作为另一个应用程序根目录.高级应用程序中的模型目录位于frontend/models",其命名空间为 frontendmodels.-- 因此,您可以在命名空间中看到app"的任何地方,将其替换为高级"模板的 frontendbackend.

<小时>

解决方案

首先,我们需要修改我们应用的配置文件以覆盖默认的 User 组件的类.我会遗漏任何不相关的内容,所以你保持你的 identityClass 定义,我只是没有展示它.我们只是添加而不是替换配置.另外,在我展示 frontend/something 的地方,对于 backend/something 也可以这样做.我只是不会一遍又一遍地重复...

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

'components' =>['用户' =>['类' =>'appcomponentsUser',//扩展用户组件],],

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

我还想指出,如果您还没有创建components"目录,则需要这样做.这是许多人添加自定义类的地方.无论是你自己的辅助类,还是扩展一个基本的 Yii 组件(比如扩展 yiiwebController).

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

user->something"* 像公共函数 getSomething()"** 所以我们可以直接在`Yii::$app->user`中使用变量和函数*/类用户扩展 yiiwebUser{公共函数 getUsername(){return Yii::$app->user->identity->username;}公共函数 getName(){返回 Yii::$app->user->identity->name;}}

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

所以现在我们可以在我们的控制器、视图、任何地方使用它......让我们测试一下.在 views/site/index.php 中,在页面底部添加:

user->name ?><?= 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

解决方案

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 apppath o. 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 appmodels. 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 frontendmodels. -- 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' => 'appcomponentsUser', // 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 yiiwebController).

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

<?php
namespace appcomponents;

use Yii;

/**
 * Extended yiiwebUser
 *
 * 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 yiiwebUser
{
    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 :)

相关文章