这就是我所拥有的:
评论控制器.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');
}
我在app/views/errors 中创建了error404.ctp和spam.ctp
问题是当评论是垃圾邮件时($result['spam'] == 1),cakePHP 会加载 error404 布局。但奇怪的是,它显示了垃圾评论的消息(“SPAM !!!”)。
当它是不存在的注释时,会加载正确的 error404 布局。
有任何想法吗?
编辑:问题已解决。代码是正确的,但必须重新启动服务器。这不应该是必要的,但这就是解决问题的原因。也许 cake 没有遵循新的 *app_error.php* 文件的正确路径。