动作成功的文本未在yii2中设置

时间:2017-05-20 17:33:12

标签: yii2

某种程度上我的actionResetPassword成功消息没有显示。有人能解释一下我在这里做错了吗?

public function actionChangePassword() 
{
    $user = User::loadModel(Yii::$app->user->identity->id);
    $user->scenario = 'changePassword';
    if ($user->load(Yii::$app->request->post())) {
        if ($user->validatePassword($user['old_password'])) {
            $user->setPassword($user['new_password']);
            $this->saveObject($user, Yii::t('app', 'Your password was changed successfully.'), Yii::t('app', 'Something is wrong'));
            $this->goHome();
        } else {
            Yii::$app->getSession()->setFlash('error', 'You have entered wrong old password');
        }
    }
    return $this->render('change-password', [
        'model' => $user,
    ]);
}

saveObject()函数:

function saveObject($object = NULL, $successMessage = '', $errorMessage = '') 
{
    if ($object->save()) {
        Yii::$app->getSession()->setFlash('success', $successMessage);
    } else {
        Yii::$app->getSession()->setFlash('error', $errorMessage);
    }
    return 1;
}

它重定向到我的主页,但不知何故没有显示我的Yii:t消息。为什么呢?

1 个答案:

答案 0 :(得分:0)

检查布局中是否有(例如:views \ layout \ main.php)

use common\widgets\Alert;
....

<?= Alert::widget() ?>

如果您没有小部件,请尝试使用

var_dump( Yii::$app->getSession()->getFlash());
在您的视图中