beforeFilter()
您可以通过在 AppController的方法中调用用户会话检查方法来执行相同操作。您的代码应如下所示:
<?php
App::uses('Sanitize', 'Utility');
App::uses('Controller', 'Controller');
class AppController extends Controller
{
public $components = array(
'Session',
'RequestHandler'
);
public $helpers = array('Html', 'Form', 'Session', 'Js' => array('Jquery'), 'Text');
public $uses = array('User');
public function beforeFilter()
{
if($this->Session->check('User.id'))
{
$this->set('logged_in', true);
$this->set('username', trim($this->Session->read('User.first_name'). ' '. $this->Session->read('User.last_name')));
}
else
{
$this->set('logged_in', false);
}
}
/*.......... Rest of your code ..............*/
}
请询问它是否不适合您。