Search code examples
phpauthenticationcakephpcakephp-2.0flash-message

CakePHP 2.6 flash messages not showing


Based on a condition in jsonresponse() function from JobsController, I want to logout user and send them to login page with a flash message Invalid user.

jsonresponse($data)

if($data['status'] == '0'){
    $this->Session->setFlash(__(INVALID_USERS_CREDENTIALS),'default', array('class' => 'error-flash-msg'));
        return $this->redirect($this->Auth->logout());
}

Now it is redirecting to login page, but flash message is not showing.

In Loginlayout

 <div style="padding-left:12%">
        <?php echo $this->Session->flash(); ?>
    </div>

    <div class="loginbox">
        <?php echo $this->fetch('content');?>
    </div>
</body>

what is the issue here, when I goto login page and enter invalid credentials, it show me flash message. But what is problem in above case...! Any help is much appreciated. Thanks.


Solution

  • You have to logout before setting the session flash message because logout() destroys the session content

    if($data['status'] == '0'){
        $this->Auth->logout()
        $this->Session->setFlash(__(INVALID_USERS_CREDENTIALS),'default', array('class' => 'error-flash-msg'));
        return $this->redirect($this->Auth->redirectUrl());
    }
    

    Or, to keep the redirection url as you did it :

    if($data['status'] == '0'){
        $redirectUrl = $this->Auth->logout()
        $this->Session->setFlash(__(INVALID_USERS_CREDENTIALS),'default', array('class' => 'error-flash-msg'));
        return $this->redirect($redirectUrl);
    }