8

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

Community
  • 1
  • 1
Cameron
  • 27,963
  • 100
  • 281
  • 483

3 Answers3

9

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!

Hoff
  • 1,762
  • 14
  • 27
  • Done & done: http://www.hoffsays.com/2012/06/12/custom-404-not-found-error-pages-in-cakephp-2-0/ – Hoff Jun 13 '12 at 00:44
  • Seems the issue I was having was because in Cake terms even in a production environment it differentiates between a notFound and a missingController exception. Might be worth mentioning that in your blog post as it caused issues for me! Also do you have a list of all the possible methods you can put in the AppExceptionRenderer? Cheers – Cameron Jun 13 '12 at 09:32
  • Found them all on the Cake website, so I've added all the exceptions here: http://bin.cakephp.org/view/513097378 to make it capable of handling all possible errors in the production environment! – Cameron Jun 13 '12 at 09:43
  • Hmm yeah it's weird - I didn't have any luck with the notFound method but for some reason the missingController and missingAction methods did it for me. Do you mind if I add the code from your pastebin to my post? I'm assuming that you've managed to get it working now? Would you mind clicking accept on the answer if it's the one that did it for ya? :) – Hoff Jun 13 '12 at 12:56
  • It's not weird, it actually makes sense as notFound is not the same as a missing controller or view so it's good they are treated as separate errors. – Cameron Jun 13 '12 at 13:10
  • So if I understand correctly notFound is only for when we're throwing our own NotFoundException, correct? Also, thanks for accepting – Hoff Jun 13 '12 at 15:17
  • This helped me so much, resolved all my issues. Although Cake docs and the developer community fail to remind that a custom exception extending CakeException will be treated as a normal Error when debug != 0 thus not passing through custom view templates unless you create customized methods including the code you wrote. Cheers. – James Jan 08 '18 at 22:49
0

This is simple,

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

That's all what you need to do

Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101
-1

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.

powtac
  • 40,542
  • 28
  • 115
  • 170
  • Yeah that calls a 404 error which is what I have posted above. But my question was that this calls the custom stuff but a bogus url does not call the custom stuff. – Cameron Jun 05 '12 at 12:53