Reputation: 3074
I'm using CakePHP 2.6.7 and copied the code to show flash messages from one controller to another, but it's not working in the second controller.
In AdminsController:
function login() {
$this->loadModel('Admin');
$this->layout = "admin-login";
// if already logged in check this step
if ($this->Auth->loggedIn()) {
return $this->redirect('dashboard'); //(array('action' => 'deshboard'));
}
// after submit login form check this step
if ($this->request->is('post')) {
if ($this->Auth->login()) {
// pr($this->Auth); exit;
if ($this->Auth->user('status') == 'active') {
// user is activated
$this->Admin->id = $this->Auth->user('id');
$this->Admin->saveField("loggedIn", 1);
return $this->redirect($this->Auth->redirectUrl());
} else {
// user is not activated
// log the user out
$msg = '<div class="alert alert-error">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>You are blocked, Contact with Adminstrator</strong>
</div>';
$this->Session->setFlash($msg);
return $this->redirect($this->Auth->logout());
}
} else {
$msg = '<div class="alert alert-error">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Incorrect email/password combination. Try Again</strong>
</div>';
$this->Session->setFlash($msg);
}
}
}
In admins/login.ctp:
<?php echo $this->Session->flash(); ?>
When I type wrong email or password it shows error message. proof: http://jegeachi.com/admins/login
But SAME task cannot be done in ResellersController. Here is the controller code:
function login() {
$this->layout = 'public-login';
$this->loadModel('Reseller');
// if already logged in check this step
if ($this->Auth->loggedIn()) {
return $this->redirect('profile'); //(array('action' => 'deshboard'));
}
// after submit login form check this step
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
} else {
$msg = '<div class="alert alert-error">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Incorrect email/password combination. Try Again</strong>
</div>';
$this->Session->setFlash($msg);
}
}
}
in Resellers/login.ctp:
<?php echo $this->Session->flash(); ?>
When login failed due to wrong email or password it is not being showed.
proof: http://jegeachi.com/resellers/login
Its a strange and wired problem. Same code work in a controller but not in other. Any idea?
Upvotes: 3
Views: 2813
Reputation: 4469
Make sure you are not already consuming the Session flash message.
This could happen if you have in your app/View/Layouts/public-login.ctp
layout something like:
<?php $this->Session->flash(); ?>
Or somewhere in your view/view blocks.
Checking the HTML output, I have found the following instead of the expected error message:
<div class="alert alert-danger display-hide">
<button class="close" data-close="alert"></button>
<span> Enter Email and password. </span>
</div>
Check your beforeRender()
callback and make sure you are not flashing the above message. If you are, it's overwriting the previous one.
Upvotes: 3
Reputation: 84
Hello I think you have forgot to echo your session message please write like below In your ctp file
echo $this->Session->flash();
Upvotes: 1