yii setflash无法在php中工作

时间:2014-03-16 07:58:14

标签: php yii

Hye那里我有一个问题我使用yii框架设计我的网站,现在当用户注册我希望显示注册成功和重定向到登录页面之类的东西。不幸的是,它保持重定向到登录页面而不显示任何消息。以下是我的用户控制器代码

public function actionCreate()
{
    $model=new User;

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['User']))
    {
        $model->attributes=$_POST['User'];
        if($model->save()) {
            $this->redirect(array('profile'));
            Yii::app()->user->setFlash('success', 'Registration successful. Please login');

        }

    }

    $this->render('create',array(
        'model'=>$model,
    ));
}

这是我登录的代码

public function actionProfile()
{
    $model=$this->loadModel(Yii::app()->user->id);
    unset($model->password);

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['User']))
    {
        $model->attributes=$_POST['User'];
        if($model->save())
            $this->redirect(array('view','id'=>$model->id));
    }

    $this->render('update',array(
        'model'=>$model,
    ));
}   

这是我的访问标尺的代码

public function accessRules()
{
    return array(
        array('allow',  // allow all users to perform 'index' and 'view' actions
            'actions'=>array('register','create'),
            'users'=>array('*'),
        ),
        array('allow', // allow authenticated user to perform 'create' and 'update' actions
            'actions'=>array('profile', 'history', 'recommendation','view'),
            'users'=>array('@'),
        ),
        array('allow', // allow admin user to perform 'admin' and 'delete' actions
            'actions'=>array('admin','delete','update','create','index'),
            'expression' => 'Yii::app()->user->isAdmin()'
        ),
        array('deny',  // deny all users
            'users'=>array('*'),
        ),
    );
}

2 个答案:

答案 0 :(得分:1)

    $this->redirect(array('profile'));
    Yii::app()->user->setFlash('success', 'Registration successful. Please login');

您在设置Flash之前重定向...而不是尝试:

    Yii::app()->user->setFlash('success', 'Registration successful. Please login');
    $this->redirect(array('profile'));

答案 1 :(得分:-1)

在您的视图中添加以下内容:

<?php if(Yii::app()->user->hasFlash('success')): ?>

<div class="success">
    <?php echo Yii::app()->user->getFlash('success'); ?>
</div>

<?php endif; ?>