CakePHP3 - 自定义页面错误404无效

时间:2016-05-18 04:04:10

标签: php cakephp

我按照http://book.cakephp.org/3.0/en/development/errors.html#exception-renderer进行教程,但它无效并显示空白页。

在config / bootstrap.php

use App\Error\AppError;
$errorHandler = new AppError();
$errorHandler->register();

在src / Error / AppError.php

<?php
namespace App\Error;

use Cake\Error\BaseErrorHandler;

class AppError extends BaseErrorHandler
{
    public function _displayError($error, $debug)
    {
        return 'There has been an error!';
    }
    public function _displayException($exception)
    {
        return 'There has been an exception!';
    }

    public function handleFatalError($code, $description, $file, $line)
    {
        return 'A fatal error has happened';
    }
}

我在src / Template / Layout / my_error.ctp中创建my_error.ctp。在我的src / Template / Error / error404.ctp中,我将布局更改为my_error.ctp。

$this->layout = 'my_error';

最后,在我的控制器中

use Cake\Network\Exception\NotFoundException;
$staff = $this->Staff->find()->where(['Staff.StaffId = '=> $id, 'Staff.PartnerId = ' =>$this->partnerId])->first();
if (empty($staff)) {
    throw new NotFoundException(__('Staff not found'));
}

2 个答案:

答案 0 :(得分:0)

每当遇到空白页面,启用调试模式时,请再次访问URL,并检查错误日志。

然而,在这种情况下的问题很可能是文档不正确/误导,因为示例应用程序错误根本不会做任何事情。 _前缀方法是protected,让它们返回无效的东西,而handleFatalError则返回布尔值。

只需查看the source of Cake\Error\BaseErrorHandlerthe core error handler Cake\Error\ErrorHandler,您要覆盖的方法就是生成输出!

您可能希望将此报告为at GitHub以上的问题。

如果您只想创建自定义4xx错误页面,那么您只需相应地修改src/Template/Error/error400.ctp模板。

答案 1 :(得分:0)

我发现了自己的错误。 :( 因为在bootstrap.php中我复制了文件末尾的代码。因此,蛋糕无法理解。请关闭此问题。感谢您的支持。

use App\Error\AppError;
$errorHandler = new AppError();
$errorHandler->register();