Cameron
Cameron

Reputation: 28803

CakePHP AppError extend functions

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

Upvotes: 0

Views: 1433

Answers (1)

Elijan
Elijan

Reputation: 1416

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)');`

Upvotes: 2

Related Questions