在 CakePHP 4 中,如何才能完全排除记录名为 MyImg 的控制器类发生的选定 MissingControllerException?我想要报告所有其他 MissingControllerException 错误(除了名为 MyImg 的 Controller 类)
您可以通过使用自定义 ErrorLogger 来实现此目的。
'Error' => [
'logger' => \App\Error\ErrorLogger::class,
'log' => true,
...
],
现在在 src/Error 文件夹中创建类 ErrorLogger:
<?php
declare(strict_types=1);
namespace App\Error;
use Cake\Controller\Exception\MissingControllerException;
use Psr\Http\Message\ServerRequestInterface;
use Throwable;
class ErrorLogger extends \Cake\Error\ErrorLogger
{
public function logException(Throwable $exception, ?ServerRequestInterface $request = null, bool $includeTrace = false): void
{
if ($request !== null) {
if ($exception instanceof MissingControllerException) {
$requestTarget = $request->getRequestTarget();
$checkUrlStarts = [
'/myimg',
];
foreach ($checkUrlStarts as $urlStart) {
if (str_starts_with($requestTarget, $urlStart)) {
// skip logging
return;
}
}
}
}
parent::logException($exception, $request, $includeTrace);
}
}