8
votes

I'm looking to create custom views for errors in CakePHP 2.1

I have been reading the following question here: CakePHP 2.0 - How to make custom error pages?

BUT there are somethings that do not work as expected!

1.) Exceptions and errors do not seem to be the same thing, as if I go to a bogus url I get the built in 404 page but if I manually do a notfound exception in the controller it will call the custom view... Why is this? I thought all errors in Cake went through the exceptions?

2.) I'm trying to render a view rather than ACTUALLY redirect the user... so for example:

App::uses('ExceptionRenderer', 'Error');

class AppExceptionRenderer extends ExceptionRenderer {
    public function notFound($error) {
        $this->controller->redirect(array('controller' => 'errors', 'action' => 'error404'));
    }
}

instead of that redirect I'm trying:

$this->controller->layout = null;
$this->controller->render('/Errors/error404');

but all I end up with is a blank page... Why is this? And this only happens when doing manual exceptions?

Can anyone answer these two questions please? Thanks

3
Blank page? Is there a PHP error? Do you run mod_security?powtac
Is there anything in the cake or PHP log file?powtac
No but why is a bogus url 404 different to a notfound exception? as one calls the custom view and one does not!Cameron
Any updates on this??? Surely someone knows how to use the ExceptionHandler in Cake 2.1?Cameron
I will ask in the IRC: freenode.cakephppowtac

3 Answers

9
votes

I've finally managed to get this figured out! Looking at the code from github, I've managed to get it working. Here's my AppExceptionRenderer.php:

App::uses('ExceptionRenderer', 'Error');

class AppExceptionRenderer extends ExceptionRenderer {
    public function missingController($error) {
        $this->controller->render('/Errors/error404', 'layout');
        $this->controller->response->send();
    }

    public function missingAction($error) {
        $this->missingController($error);
    }
}

If you want to call your controller callbacks you'll have to do something like this as well before beforeFilter():

$this->controller->beforeFilter();

That $this->controller->response->send(); line is the kicker. Hopefully this works for you!

0
votes

This is simple,

public function notFound($error) {
    $this->_outputMessage('error404');
}

That's all what you need to do

-1
votes

Simply:

throw new NotFoundException;

See the example code in the documentation about "Built in Exceptions".

App::uses('ExceptionRenderer', 'Error'); should not be required.

For an individual view edit View/Errors/error400.ctp.