php Yii2 - 如何从 Yii::$app->user 获取当前用户名或名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38415388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Yii2 - How to get the current username or name from Yii::$app->user?
提问by Wade
How do you add, or get, variables using Yii::$app->user->something
?
您如何使用 来添加或获取变量Yii::$app->user->something
?
I don't want to get them through identity
. I want to make specific ones accessible through user
.
我不想让他们通过identity
。我想通过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
回答by Wade
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!
虽然 SO 上的其他主题提供了一个 chincy hack 解决方案,或者只是通过 获取它Yii::$app->user->identity
,但我将向您展示如何正确地做到这一点。您可以通过几乎扩展所有内容来使 Yii2 随心所欲!
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.
命名空间速成课程:基本上,你有namespace app\path\to
. 这是文件所在的位置。例如,您的 ContactForm.php 位于应用程序根models
目录中,因此它的命名空间是app\models
. 在高级中,您有多个应用程序(前端和后端),因此应用程序根目录不同,因此您拥有frontend
一个应用程序根目录和backend
另一个。高级应用程序中的模型目录位于“frontend/models”,其命名空间为frontend\models
. -- 所以你在命名空间中看到“app”的任何地方,你都可以用“高级”模板替换它frontend
或替换它backend
。
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...
首先,我们需要修改应用程序的配置文件以覆盖默认的 User 组件的类。我会遗漏任何不相关的东西,所以你保持你的identityClass
定义,我只是不展示它。我们只是添加而不是替换配置。此外,在我展示的地方frontend/something
,也可以对backend/something
. 我只是不会一遍又一遍地重复......
Basic (config/web.php
) -or- Advanced (frontend/config/main.php
)
基本 ( config/web.php
) - 或 - 高级 ( 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
!
请记住:如果您使用的是高级模板,请替换app
为frontend
或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
).
我还想指出,如果您还没有创建“components”目录,则需要这样做。这是许多人添加自定义类的地方。无论是你自己的助手类,还是扩展一个基本的 Yii 组件(例如 extends yii\web\Controller
)。
Create a new file in your components
directory named User.php
. In that file, place the following code:
在您的components
目录中创建一个名为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;
}
}
Note: username
and name
must be valid column names in your database's user table!
注意:username
并且name
必须是数据库用户表中的有效列名!
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:
所以现在我们可以在我们的控制器、视图、任何地方使用它......让我们测试一下。在 中views/site/index.php
,将其添加到页面底部:
<?= \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 :)
如果一切正常,并且您在用户表中有名称和用户名的值,它们应该打印到页面上:)