You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TYPO3 10.4中如何为getSiteByIdentifier报错配置自定义500错误页

解决TYPO3 10.4中SiteNotFoundException(错误码1521716628)的自定义错误页面问题

方法1:自定义全局异常处理器捕获渲染

利用TYPO3的异常处理机制,针对特定错误码的SiteNotFoundException返回自定义页面:

  1. 创建自定义异常处理类
    在你的扩展(如site_error_handler)下新建Classes/Error/SiteExceptionHandler.php:
<?php
namespace Vendor\SiteErrorHandler\Error;

use TYPO3\CMS\Core\Error\PageErrorHandler\PageErrorHandlerInterface;
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
use TYPO3\CMS\Core\Http\HtmlResponse;
use TYPO3\CMS\Core\Http\Response;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class SiteExceptionHandler implements PageErrorHandlerInterface
{
    protected $configuration = [];

    public function __construct(array $configuration)
    {
        $this->configuration = $configuration;
    }

    public function handlePageError(int $statusCode, string $message, array $exception = null): Response
    {
        // 仅处理目标异常
        if ($exception instanceof SiteNotFoundException && $exception->getCode() === 1521716628) {
            // 加载自定义模板内容
            $templatePath = GeneralUtility::getFileAbsFileName($this->configuration['customTemplate']);
            $content = file_get_contents($templatePath);
            // 返回自定义页面,可根据需求调整状态码(示例用404)
            return new HtmlResponse($content, 404);
        }
        // 其他异常交给默认处理器
        $defaultHandler = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Error\PageErrorHandler\DefaultErrorHandler::class, $this->configuration);
        return $defaultHandler->handlePageError($statusCode, $message, $exception);
    }
}
  1. 配置LocalConfiguration.php
    在typo3conf/LocalConfiguration.php的SYS节点下添加配置:
'SYS' => [
    // ...原有配置
    'exceptionHandler' => 'Vendor\SiteErrorHandler\Error\SiteExceptionHandler',
    'pageErrorHandler' => [
        'errorCode' => [
            '500' => [
                'handler' => 'Vendor\SiteErrorHandler\Error\SiteExceptionHandler',
                'customTemplate' => 'EXT:site_error_handler/Resources/Private/Templates/CustomSiteError.html',
            ],
        ],
    ],
],
  1. 创建自定义错误模板
    在EXT:site_error_handler/Resources/Private/Templates/CustomSiteError.html中写入带样式的HTML:
<!DOCTYPE html>
<html>
<head>
    <title>站点不存在</title>
    <style>
        body { font-family: Arial, sans-serif; text-align: center; padding: 50px; background-color: #f8f9fa; }
        .error-box { max-width: 600px; margin: 0 auto; padding: 30px; background: white; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
        h1 { color: #dc3545; margin-bottom: 20px; }
        p { color: #6c757d; font-size: 16px; }
    </style>
</head>
<body>
    <div class="error-box">
        <h1>抱歉,请求的站点不存在</h1>
        <p>我们无法找到您访问的站点,请检查请求参数是否正确。</p>
    </div>
</body>
</html>

方法2:XCLASS重写SiteFinder( fallback站点方案)

通过XCLASS替换核心SiteFinder类,当目标站点不存在时自动切换到预设的fallback站点:

  1. 注册XCLASS
    在扩展的ext_localconf.php中添加:
$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][\TYPO3\CMS\Core\Site\SiteFinder::class] = [
    'className' => \Vendor\SiteErrorHandler\Xclass\CustomSiteFinder::class
];
  1. 编写重写类
    创建Classes/Xclass/CustomSiteFinder.php:
<?php
namespace Vendor\SiteErrorHandler\Xclass;

use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Exception\SiteNotFoundException;

class CustomSiteFinder extends SiteFinder
{
    public function getSiteByIdentifier(string $identifier): \TYPO3\CMS\Core\Site\Site
    {
        try {
            return parent::getSiteByIdentifier($identifier);
        } catch (SiteNotFoundException $e) {
            // 仅处理目标错误码的异常
            if ($e->getCode() === 1521716628) {
                try {
                    // 切换到预设的fallback站点
                    return parent::getSiteByIdentifier('fallback');
                } catch (SiteNotFoundException $fallbackException) {
                    // fallback站点不存在时抛出原异常
                    throw $e;
                }
            }
            throw $e;
        }
    }
}
  1. 创建fallback站点
    在TYPO3后台新建站点,设置标识符为fallback,并将根页面设置为包含自定义错误内容的页面。

关键注意事项

  • 禁止直接修改TYPO3核心文件(如sysext/core/Classes/Site/SiteFinder.php),避免更新时丢失修改且不符合规范。
  • 使用异常处理器时,建议返回404状态码(而非默认500),更符合“站点不存在”的错误场景。
  • XCLASS方法需确保扩展激活,且兼容TYPO3 10.4版本。

内容的提问来源于stack exchange,提问作者Maciej

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 12:14:59