为 TYPO3 9.5 中的 404 页面设置 content-type HTTP header
Set content-type HTTP header for 404 page in TYPO3 9.5
我正在使用 TYPO3 制作网络服务。前端的所有内容都应该是 JSON 和 HTTP header Content-Type: application/json
,但是我无法更改 404 页面的 Content-Type
header。无论我尝试什么,它总是 Content-Type: text/html; charset=utf-8
。我该如何更改?
这是我的基本 page
TypoScript 配置:
page = PAGE
page {
config {
disableAllHeaderCode = 1
disablePrefixComment = 1
xhtml_cleaning = 0
admPanel = 0
}
10 = USER
10 {
userFunc = TYPO3\CMS\Extbase\Core\Bootstrap->run
extensionName = MyExt
pluginName = MyPlugin
vendorName = MyVendor
}
}
扩展程序使用 TYPO3\CMS\Extbase\Mvc\View\JsonView
。
我已经尝试使用 config.additionalHeaders.10.header = Content-Type: application/json
添加 header。我还尝试将 [FE][pageNotFound_handling]
设置为 USER_FUNCTION:...
并在 PHP.
中设置 headers
我自己找到了解决方案。从 TYPO3 9.5 开始,默认页面未找到处理可以在站点配置中被覆盖:
errorHandling:
-
errorCode: 404
errorHandler: PHP
errorPhpClassFQCN: Vendor\MyExt\PageErrorHandler\PageNotFoundHandler
并且在 EXT:MyExt/Classes/PageErrorHandler/PageNotFoundHandler.php:
<?php
namespace Vendor\MyExt\PageErrorHandler;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Error\PageErrorHandler\PageErrorHandlerInterface;
/**
* Class PageNotFoundHandler
*/
class PageNotFoundHandler implements PageErrorHandlerInterface
{
/**
* Handle page error
*
* @param ServerRequestInterface $request
* @param string $message
* @param array $reasons
* @return ResponseInterface
*/
public function handlePageError(ServerRequestInterface $request, string $message, array $reasons = []): ResponseInterface
{
$response = new Response(404, ['Content-Type' => 'application/json'], '{"error":"Not found"}');
return $response;
}
}
我正在使用 TYPO3 制作网络服务。前端的所有内容都应该是 JSON 和 HTTP header Content-Type: application/json
,但是我无法更改 404 页面的 Content-Type
header。无论我尝试什么,它总是 Content-Type: text/html; charset=utf-8
。我该如何更改?
这是我的基本 page
TypoScript 配置:
page = PAGE
page {
config {
disableAllHeaderCode = 1
disablePrefixComment = 1
xhtml_cleaning = 0
admPanel = 0
}
10 = USER
10 {
userFunc = TYPO3\CMS\Extbase\Core\Bootstrap->run
extensionName = MyExt
pluginName = MyPlugin
vendorName = MyVendor
}
}
扩展程序使用 TYPO3\CMS\Extbase\Mvc\View\JsonView
。
我已经尝试使用 config.additionalHeaders.10.header = Content-Type: application/json
添加 header。我还尝试将 [FE][pageNotFound_handling]
设置为 USER_FUNCTION:...
并在 PHP.
我自己找到了解决方案。从 TYPO3 9.5 开始,默认页面未找到处理可以在站点配置中被覆盖:
errorHandling:
-
errorCode: 404
errorHandler: PHP
errorPhpClassFQCN: Vendor\MyExt\PageErrorHandler\PageNotFoundHandler
并且在 EXT:MyExt/Classes/PageErrorHandler/PageNotFoundHandler.php:
<?php
namespace Vendor\MyExt\PageErrorHandler;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Error\PageErrorHandler\PageErrorHandlerInterface;
/**
* Class PageNotFoundHandler
*/
class PageNotFoundHandler implements PageErrorHandlerInterface
{
/**
* Handle page error
*
* @param ServerRequestInterface $request
* @param string $message
* @param array $reasons
* @return ResponseInterface
*/
public function handlePageError(ServerRequestInterface $request, string $message, array $reasons = []): ResponseInterface
{
$response = new Response(404, ['Content-Type' => 'application/json'], '{"error":"Not found"}');
return $response;
}
}