自定义蛋糕错误
这就是我所拥有的:
comments_controller.php
if (empty($result)) { //$result is empty if comment does not exist
$this->cakeError('error404', array('message'=>'Comment not found'));
} elseif ($result['spam'] == 1) {
$this->cakeError('spam', array('message'=>'SPAM!!!'));
}
app_error.php
function error404($params) {
$this->controller->set('title', 'Page not found');
$this->controller->set('message', $params['message']);
$this->_outputMessage('error404');
}
function spam($params) {
$this->controller->set('title', 'Spam');
$this->controller->set('message', $params['message']);
$this->_outputMessage('spam');
}
我创建了 error404.ctp 和 spam.ctp app/views/errors 里面的
问题是,当评论是垃圾邮件时 ($result['spam'] == 1),cakePHP 会加载 error404 布局。但奇怪的是,它显示了垃圾评论的消息(“SPAM!!!”)。
当是不存在的注释时,加载正确的error404布局。
有什么想法吗?
编辑:问题已解决。代码是正确的,但是必须重新启动服务器。这不应该是必要的,但这就是解决问题的方法。也许 cake 没有遵循新的 *app_error.php* 文件的正确路径。
This is what I have:
comments_controller.php
if (empty($result)) { //$result is empty if comment does not exist
$this->cakeError('error404', array('message'=>'Comment not found'));
} elseif ($result['spam'] == 1) {
$this->cakeError('spam', array('message'=>'SPAM!!!'));
}
app_error.php
function error404($params) {
$this->controller->set('title', 'Page not found');
$this->controller->set('message', $params['message']);
$this->_outputMessage('error404');
}
function spam($params) {
$this->controller->set('title', 'Spam');
$this->controller->set('message', $params['message']);
$this->_outputMessage('spam');
}
And I created error404.ctp and spam.ctp inside app/views/errors
The problem is that when the comment is a spam ($result['spam'] == 1), cakePHP loads the error404 layout. But strangely, it shows the message for a spam comment ("SPAM!!!").
When it is a comment that does not exist, the correct error404 layout is loaded.
Any ideas?
EDIT: Problem fixed. The code was right, but the server had to be restarted. It shouldn't be necessary, but this was what fixed the problem. Maybe cake was not following the correct path of the new *app_error.php* file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我很快就尝试了你的设置(CakePHP 1.3),它工作得很好。
我最好的猜测是您的文件名或类名之一有拼写错误,并且它正在使用 error404,因为它找不到某些内容。
检查完后,我将打开调试器,或者您可以开始抛出
语句,直到计算出执行顺序。
I very quickly tried your setup (CakePHP 1.3) and it worked fine.
My best guess is that you have a typo in either one of the filenames or class names and it is using error404 because it can't find something.
After checking this I would turn on the debugger, or you can start throwing in
statements until you work out the execution order.