如何在生产环境中使用 cakephp 3 捕获 404 错误

How to catch the 404 error with cakephp 3 in a productive environment

我想在我的新蛋糕应用程序中捕获所有 404 错误(找不到控制器)。但是我不知道怎么办。

有什么配置吗?还是我必须自己捕获抛出的错误?如果有,在哪里?

这是一种可行的方法。定义您自己的错误处理程序,它扩展了默认的 ErrorHandler

<?php
// Custom Handler - goes in src/Error/AppError.php
namespace App\Error;

use Cake\Routing\Exception\MissingControllerException;
use Cake\Error\ErrorHandler;

class AppError extends ErrorHandler
{
    public function _displayException($exception)
    {
        if ($exception instanceof MissingControllerException) {
            // Here handle MissingControllerException by yourself
        } else {
            parent::_displayException($exception);
        }
    }
}

然后将此处理程序注册为默认处理程序。

// Register handler in config/bootstrap.php
use App\Error\AppError;

$errorHandler = new AppError();
$errorHandler->register();

http://book.cakephp.org/3.0/en/development/errors.html