douping7105 2011-04-22 10:57
浏览 8

CakePHP AppError扩展函数

I have the following code as my AppError file on my CakePHP site:

<?php

class AppError extends ErrorHandler
{
    function _outputMessage($template)
    {
        $this->controller->render($template, 'error');
        $this->controller->afterFilter();
        echo $this->controller->output;
    }

    function error404 ()
    {
        $this->set('title_for_layout', 'Not Found (404)');
    }
}   

?>

The first function works fine by making all errors use the error layout instead of the default one. But the second function causes the App to blow up... why? Shouldn't it just extend the error404 function inside the error handler?

Thanks

  • 写回答

1条回答 默认 最新

  • dongyue934001 2011-04-22 11:36
    关注

    It extends the class and by that AppError inherits all the methods from ErrorHandler.So if you don't have a method error404() , the AppError object will call its parent method, in this case error404 in ErrorHandler;

    However, if you define the method that already exits in ErrorHandler it will not 'extend it' but 'override it.' In other words: If you create method error404 and this method is called by AppError object it will call its version of error404()

    Now, if you look at error404 function inside ErrorHandler

     function error404($params) {
         extract($params);
    
         if (!isset($url)) {
             $url = $action;
         }
        if (!isset($message)) {
             $message = '';
         }
         if (!isset($base)) {
             $base = '';
         }
    
         header("HTTP/1.0 404 Not Found");
         $this->error(array('code' => '404',
                             'name' => 'Not found',
                            'message' => sprintf("The requested address %s was not found on this server.", $url, $message),
                             'base' => $base));
        exit();
     }
    

    you can see that there is certain behavior and params are expected in this function. So that's why your app is crashing, somewhere AppError is calling error404 expecting the behavior from parent (ErrorHandler) class.Try to mimic the method (by including params and exit at the end).

    Also $this->set('title_for_layout', 'Not Found (404)'); I think it should be

     $this->controller->set('title_for_layout', 'Not Found (404)');`
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c