yii setflash is not working when using redirect

2019-02-28 07:27发布

Hye there i have a problem i've design my website using yii framework and now when user register i want to show something like registration successful and redirect to log in page. Unfortunately it keep redirect to log in page without showing any message. Below is my code for user controller

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,
    ));
}

This is the code for my log in

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,
    ));
}   

This is the code for my access ruler

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('*'),
        ),
    );
}

This is in my actionview

public function actionView($id)
{
    $this->render('view',array(
        'model'=>$this->loadModel($id),
    ));
}

4条回答
beautiful°
2楼-- · 2019-02-28 07:58

I have just found out my mistake:

instead of this:

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

I should do like this:

Yii::app()->user->setFlash('success', 'Registration successful. Please login');
      $this->redirect(array('/site/login'));
查看更多
Melony?
3楼-- · 2019-02-28 07:59

It happened the same, and it turned out that was not going to the "if" because it is not being fulfilled and why not show the message or redirected to another view.

In short, it could perhaps be because you are not fulfilling the condition. ;)

查看更多
何必那么认真
4楼-- · 2019-02-28 08:09

untested but:

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

    }

should be

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

You also need to check for the flash message in your view to display it }

查看更多
老娘就宠你
5楼-- · 2019-02-28 08:14

Use return because it stopped process.

return $this->redirect();
查看更多
登录 后发表回答