我的变量loggedIn
我AppController
被定义在beforeFilter()
功能,如下所示:通行证变量CakePHP中
function beforeFilter(){
$this->Auth->loginRedirect = array('controller'=> 'questions', 'action' => 'home');
$this->Auth->logoutRedirect = array('controller'=> 'questions', 'action' => 'home');
$this->Auth->allow('signup', 'confirm', 'home', 'show');
$this->Auth->authorize = 'controller';
$this->Auth->userScope = array('User.confirmed' => '1');
$this->set('loggedIn', $this->Auth->user('id'));
}
在我的布局,我使用下面的测试loggedIn
变量的值:
<?php if($loggedIn): ?>
当我运行该应用程序我得到这个错误:
Undefined variable: loggedIn [APP\View\Layouts\default.ctp
你能帮助我吗?先谢谢你。
您可以在视图中使用'$ this-> Auth-> user('id')'! –
您还应该在函数内添加'parent :: beforeFilter()'。 (并不是说它会有太大变化) – cornelb
@GilbertoRamos不,这是不可能的,'Auth'是一个组件,而不是视图帮助器。从CakePHP 2开始,可以使用[**'AuthComponent:user()'**](http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#accessing-the-登录用户)。 – ndm