I am new to CakePHP, and despite searching high and low I can’t find an answer to what seems like a trivial problem. The issue is that after logging in through my UserController I am losing the data found in $this->Auth->user() on all other Controllers. Any views not associated with UserController will always have null data and $this->Auth->loggedIn() will return false.
In my AppController class, I have attempted to save the data into a variable inside beforeFilter() so that I may check the variable in my views, but it doesn’t make a difference. Here is the relavent code for my UserController and AppController classes:
class AppController extends Controller {
// Pass settings in $components array
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'home', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'home', 'action' => 'index')
)
);
public function beforeFilter() {
//debug($this->Auth->user());
$this->set('loggedIn', $this->Auth->user());
}
}
Users class:
App::uses('AppController', 'Controller');
class UsersController extends AppController {
public $helpers = array('Html', 'Form');
public $name = 'Users';
public function beforeFilter() {
parent::beforeFilter();
}
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
}
}
}
public function logout() {
return $this->redirect($this->Auth->logout());
}
}
I have also made sure to use parent::beforeFilter() in my other controllers.
I believe I found the solution. In my HomeController, which was the page I was always viewing after I logged in, it seems as though there was some invisible character following the closing php declaration
?>(which was not needed either) so I deleted it and the?>and voila, everything is working as expected.I was tipped off that something was amiss after I tried manually using
session_start()in my layout and I was given an error about the line after?>in the HomeController class.