view - Yii2:从 View 中调用用户类中的自定义方法

标签 view model yii2 yii2-user

我想扩展我自己的用户模型,使用一个名为 getFirstname 的函数。
该函数应返回数据库中的自定义字段。

但是当我扩展用户模型时。它说“调用未知方法:yii\web\User::getFirstname()”

我的用户模型在 app\models\user 中。
我已删除文件中与此问题无关的方法。

<?php

namespace app\models;

use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;

/**
 * User model
 *
 */
class User extends ActiveRecord implements IdentityInterface
{

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%user}}';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [];
    }

    /**
     * Finds user by email
     *
     * @param string $email
     * @return static|null
     */
    public static function findByEmail($username)
    {
        return static::findOne(['user_email' => $username]);
    }

    /**
     * @inheritdoc
     */
    public function getId()
    {
        return $this->getPrimaryKey();
    }

    public function getFirstname()
    {
        return $this->user_firstname;
    }  
}

我的配置文件:

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'components' => [
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'thepaXucufrE2pefRethaxetrusacRu3',
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'user' => [
            'identityClass' => 'app\models\User',
            'loginUrl' => ['login/sign-in'],
            'enableAutoLogin' => true,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => true,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => require(__DIR__ . '/db.php'),
    ],
];

在我的 View 文件中:
<?= Yii::$app->user->getFirstname() ?>

我在这里做错了什么?

最佳答案

试试这个:

<?= Yii::$app->user->identity->getFirstname() ?>

<!-- or -->

<?= Yii::$app->user->identity->firstname ?>

Yii::$app->user您只需获取用户组件。 User component类评论告诉你:

User is the class for the "user" application component that manages the user authentication status.



所以它不是您通过 Yii::$app->user 获得的实际用户。 ,它是管理组件。与 identitygetIdentity()从此类中,您将获得实现 IdentityInterface 的用户对象。当您查看时:您的 User 类实现了此接口(interface)。在您的配置中,您告诉用户组件它应该完全使用您的用户类。

关于view - Yii2:从 View 中调用用户类中的自定义方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28953232/

相关文章:

Django 模型 - 每个父引用的子表元素的自动增量 id

php - Yii2:如何创建自定义删除按钮?

javascript - 无法通过 xhr 请求将 post 参数传递给 yii2 Controller

asp.net-mvc-3 - 在 ASP .NET MVC 3 中过滤索引 View ?

mysql - 在带有 LIKE 的 CASE 语句中使用 GROUP_CONCAT 列别名

c# - 使用 Entity Framework 数据库优先数据库上下文与 mvvm View 模型类

python - 使用 Flask-SQLAlchemy 和 Python 进行建模

php - 添加 Materialize css 后尝试在 Yii2 上获取非对象错误的属性

android - 在 JetPack Compose 中手动重构所有 AndroidView

java - 获取当前设置的内容 View