retrieving user information from auth component
I have problems checking 开发者_JS百科if user is logged in from any controller other than UsersController.
I was trying to make a user profile page. I routed it to PagesController and when I use
$this->Auth->user()
it always returns null. But if I route it to UsersController it works fine. Why is that? Login seems to work ok.
Your app_controller.php :
class AppController extends Controller {
var $components = array('Auth', 'Session', 'Cookie');
function beforeFilter() {
$this->Auth->authError = 'blabla';
$this->Auth->loginError = 'blabla';
}
}
In your controllers :
function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('function1', 'function2', 'function3'); //etc
$this->Auth->autoRedirect = true;
}
It's because you include the Auth component only in your User's controller. You should include it in AppController and in beforeFilter(). This way you will have access in all controllers.
精彩评论