【问题标题】:Yii2: call custom method in class User from viewYii2:从视图中调用用户类中的自定义方法
【发布时间】:2015-05-11 06:31:31
【问题描述】:

我想扩展我自己的用户模型,使用一个名为 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'),
    ],
];

在我的视图文件中:

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

我在这里做错了什么?

【问题讨论】:

    标签: view model yii2 yii2-user


    【解决方案1】:

    试试这个:

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

    使用Yii::$app-&gt;user,您只需获得用户组件User component 类评论告诉你:

    User 是管理“用户”应用程序组件的类 用户认证状态。

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

    【讨论】:

    • 有效!感谢您的详细描述!
    【解决方案2】:

    具有getFirstname 方法的User 类位于\app\models 命名空间中,但在您的视图中调用的类位于yii\web 命名空间中。您应该更改代码以使用\app\models\User

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-17
      • 2015-10-03
      • 2023-04-02
      • 1970-01-01
      • 2017-08-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多