我想摆脱 Auth 组件错误消息,特别是每当我尝试访问不允许的操作时出现的 authError 消息。
为了确定,我仔细检查布局中的任何地方都没有 $this->Session->flash()
调用。此外,设置空值也不起作用,因为组件具有默认消息值。
我在 AppController 类中使用具有以下配置的 Auth 组件:
class AppController extends Controller {
var $components = array(
'Auth' => array(
'userModel' => 'WebUser',
'loginAction' => '/login',
'loginRedirect' => '/',
'logoutRedirect' => '/login',
'autoRedirect' => false,
),
'Session',
...
...
}
对于login 和logout 重定向,我设置了两条路由:
Router::connect('/', array('controller' => 'posts', 'action' => 'index'));
Router::connect('/login', array('controller' => 'web_users', 'action' => 'login'));
WebUser Controller 中的login 操作几乎是空的;我只更改默认布局:
function login() {
$this->layout = 'login';
$this->set('title_for_layout', 'Sign in');
}
最后,我有一个非常简单的 login.ctp 布局文件:
<html>
<head>
...
</head>
<body>
...
<?php echo $content_for_layout; ?>
...
</body>
</html>
当我访问 http://example.com/login
时没有问题,没有消息,只有登录表单。但是,在请求任何其他操作时,我收到默认的 authError 消息,就在 Auth 组件重定向到 login 操作之后。出现两个问题:
为什么在任何地方都没有调用(请参阅下面的更新 2)<$this->Session->flash()
时,Auth 组件会显示 flash 消息?- 如何在 authError 属性中设置空值?
谢谢!
更新
我想出了一个非常丑陋的解决方案:我创建了一个元素 login_error.ctp 并分配给 Auth 组件初始化中的 flashElement 属性:
class AppController extends Controller {
var $components = array(
'Auth' => array(
'flashElement' => 'login_error',
...
...
}
在 login_error.ctp 中,我只是与 authError 默认消息进行比较:
<?php if ( $message !== 'You are not authorized to access that location.' ): ?>
<div id="flashMessage" class="message"><?php echo $message; ?></div>
<?php endif; ?>
它有效,但我讨厌它!
更新 2
感谢 dogmatic69 的回答,我强制自己再次检查所有内容。我终于找到了调用 $this->Session->flash()
的位置。它是在我之前写的一个小 View 元素上。它与登录/注销无关,所以我没有注意那个文件。
更新 3
感谢SpawnCxy answer以及。复制 Auth 组件并进行自定义修改是比字符串比较更好的方法。
最佳答案
只需从您的 View /布局中删除 $this->Session->flash('auth')
。
关于cakephp - 避免 CakePHP 的 Auth 组件显示身份验证错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4950417/