2

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

About creating custom views for exception handling in CakePHP 2.0+ and have been using it as a base to start doing the same in my own application hence starting my own question.

However I'm not following the logic. For example how does the Throw NotFoundException know to call the notFound method in the Errors Controller as I don't see any direct relationship in terms of the naming... Unless I'm missing the point?

In any case I'm looking to add 404, 403, and 401 errors and then be able to create custom views and call them using the exception handler throughout my app.

Can anyone shed more light on this? I'm using the latest version of Cake 2.1

So I have the following code:

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

    class AppExceptionRenderer extends ExceptionRenderer {

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

And I want to replace that redirect with rendering a custom error view:

I've tried:

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

But that just shows a blank page... Can anyone help me out as I don't want to do the redirect and would much rather follow conventions and render actual views with the same url when getting errors.

Update: Also noticed that the custom views ONLY get called when exceptions are manually called in the controller, and not for actual errors such as domain.com/garbageurl or something else... So it doesn't seem to be doing what I thought!

4

2 回答 2

1

看看 core Cake 中的这些文件:

这是正在发生的事情:

  • ErrorHandler::handleException()是您的异常处理程序。它在抛出异常时被调用。
  • ErrorHandler::handleException()调用ExceptionRenderer::__construct()(您的自定义异常渲染器必须扩展ExceptionRenderer),它解析抛出的异常的名称,并从中设置$this->method
  • ErrorHandler::handleException()然后调用ExceptionRenderer::render()which 使用call_user_func_array()来调用名称为 的方法$this->method
于 2012-05-31T21:44:12.130 回答
0

我只是在寻找同样的东西,但找不到使用AppExceptionRenderer. 它只是不允许您拥有单独的 error403 和 error404 模板文件。

所以我只是在我的/app/View/Errors/error400.ctp 文件中做了这个......

<? if ($error instanceof ForbiddenException) { ?>

    <h4>Whoops! The page you attempted to access 
        requires permissions that you don't have.</h4>  

<? } else { ?>

    <h4>Whoops! We couldn't find the page you were looking for, sorry!</h4> 

<? } ?>
于 2013-02-19T02:47:12.840 回答