首页 > 解决方案 > TYPO3 routeEnhancers 在根页面上带有后缀“.html”

问题描述

如果 routeEnhancers 配置了“.html”后缀,是否仍然没有选择像“www.mysite.com”这样的 baseurl?

在我看来,这应该是一个基本功能,但我找不到任何解决方案。重定向主页链接不是一种选择,因为规范仍然指向错误的 URL (www.mysite.com/index.html)

有什么解决办法吗?

我的配置如下所示:

routeEnhancers:
  PageTypeSuffix:
    type: PageType
    default: '.html'
    index: index
    map:
      .html: 0

标签: typo3url-routingslugtypo3-9.x

解决方案


forge.typo3.org 上报告的问题仍然存在(截至 2019 年 9 月)。

暂时,您可以提供一个自定义的 PageType 装饰器来实现所需的结果。报告此问题的开发人员 Daniel Dorndorf 发布了源代码:

/Classes/Routing/Enhancer/CustomPageTypeDecorator.php

<?php

namespace Brand\Extensionname\Classes\Routing\Enhancer;

use TYPO3\CMS\Core\Routing\Enhancer\PageTypeDecorator;
use TYPO3\CMS\Core\Routing\RouteCollection;

/**
 * Class CustomPageTypeDecorator
 */
class CustomPageTypeDecorator extends PageTypeDecorator
{
    public const IGNORE_INDEX = [
        '/index.html',
        '/index/',
    ];

    public const ROUTE_PATH_DELIMITERS = ['.', '-', '_', '/'];

    /**
     * @param \TYPO3\CMS\Core\Routing\RouteCollection $collection
     * @param array $parameters
     */
    public function decorateForGeneration(RouteCollection $collection, array $parameters): void
    {
        parent::decorateForGeneration($collection, $parameters);

        /**
         * @var string $routeName
         * @var \TYPO3\CMS\Core\Routing\Route $route
         */
        foreach ($collection->all() as $routeName => $route) {
            $path = $route->getPath();

            if (true === \in_array($path, self::IGNORE_INDEX, true)) {
                $route->setPath('/');
            }
        }
    }
}

ext_localconf.php

<?php
defined('TYPO3_MODE') or die();

// Register custom PageTypeDecorator:
$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['enhancers'] += ['CustomPageType' => \Brand\Extensionname\Classes\Routing\Enhancer\CustomPageTypeDecorator::class];

将此添加到您的模板扩展中,调整 PHP 命名空间 ( \Brand\Extensionname\),您就完成了。

配置.yaml

PageTypeSuffix:
  type: CustomPageType
  default: '.html'
  index: 'index'
  map:
    '.html': 0

推荐阅读