自定义蛋糕错误

发布于 2024-10-30 22:30:52 字数 1152 浏览 0 评论 0原文

这就是我所拥有的:

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.ctpspam.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沦落红尘 2024-11-06 22:30:52

我很快就尝试了你的设置(CakePHP 1.3),它工作得很好。

我最好的猜测是您的文件名或类名之一有拼写错误,并且它正在使用 error404,因为它找不到某些内容。

检查完后,我将打开调试器,或者您可以开始抛出

die('got here: ' . __METHOD__ . __LINE__ );

语句,直到计算出执行顺序。

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

die('got here: ' . __METHOD__ . __LINE__ );

statements until you work out the execution order.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文