我想在我的新蛋糕应用程序中捕获所有404错误(未找到控制器)。但我不知道怎么做。
有没有配置?或者我必须自己抓住抛出的错误?如果是这样,在哪里?
答案 0 :(得分:2)
这是一种可行的方法。定义自己的错误处理程序,扩展默认的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();