似乎什么都没有发生的原因是因为您没有在视图中显示 Acl 组件生成的 flash 消息(正如 Dave 在评论中已经提到的那样)。您必须将其添加到您的布局或视图中:
echo $this->Session->flash('auth');
这样,用户将看到您authError
在AuthComponent
.
另一种方法是在 AppController 的 beforeFilter 方法中运行 Acl 检查,如果 Acl 检查失败,则重定向用户,如下所示:
/**
* ACL Check (CakeError controller is exempt from this example,
* so errors are always "allowed" to be shown).
*/
if (!is_null($this->Auth->User()) && $this->name != 'CakeError'
&& !$this->Acl->check(array(
'model' => 'User',
'foreign_key' => AuthComponent::user('id')),
$this->name . '/' . $this->request->params['action']
)) {
// Optionally log an ACL deny message in auth.log
CakeLog::write('auth', 'ACL DENY: ' . AuthComponent::user('username') .
' tried to access ' . $this->name . '/' .
$this->request->params['action'] . '.'
);
// Render the forbidden page instead of the current requested page
echo $this->render('/Pages/forbidden');
/**
* Make sure we halt here, otherwise the forbidden message
* is just shown above the content.
*/
exit;
}
这样,app/View/Pages/forbidden.ctp
文件将被呈现,exit 语句将阻止重定向发生。