要始终添加附加 /,您可以自己创建一个路由增强器装饰器并将其放入您的站点包中。
在您的站点包中创建一个文件,Classes/Routing/Enhancer/ForceAppendingSlashDecorator.php
内容如下:
<?php
declare(strict_types=1);
namespace MyVendor\SitePackage\Routing\Enhancer;
use TYPO3\CMS\Core\Routing\Enhancer\AbstractEnhancer;
use TYPO3\CMS\Core\Routing\Enhancer\DecoratingEnhancerInterface;
use TYPO3\CMS\Core\Routing\RouteCollection;
class ForceAppendingSlashDecorator extends AbstractEnhancer implements DecoratingEnhancerInterface
{
/**
* {@inheritdoc}
*/
public function getRoutePathRedecorationPattern(): string
{
return '\/$';
}
/**
* {@inheritdoc}
*/
public function decorateForMatching(RouteCollection $collection, string $routePath): void
{
foreach ($collection->all() as $route) {
$route->setOption('_decoratedRoutePath', '/' . trim($routePath, '/'));
}
}
/**
* {@inheritdoc}
*/
public function decorateForGeneration(RouteCollection $collection, array $parameters): void
{
foreach ($collection->all() as $routeName => $existingRoute) {
$existingRoutePath = rtrim($existingRoute->getPath(), '/');
$existingRoute->setPath($existingRoutePath . '/');
}
}
}
请替换设置与您的站点包匹配的正确命名空间。
要注册您的路由增强器,请将以下行添加到您的ext_localconf.php
:
$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['enhancers']['ForceAppendingSlash'] = \MyVendor\SitePackage\Routing\Enhancer\ForceAppendingSlashDecorator::class;
最后一步,将以下代码放入您的站点配置 yaml 文件中:
routeEnhancers:
PageTypeSuffix:
type: ForceAppendingSlash
在此调整之后,TYPO3 将始终添加附加 / 到您的 URL,以便新 URL 将匹配由 realurl 创建的旧 URL。