如何为 TYPO3 9 LTS 编写路由方面映射器

How do I write a routing aspect mapper for TYPO3 9 LTS

我需要自定义方面映射器 class,以定义可选 get 参数的值。此参数包含带有额外数据的 cf_cache 标识符。但是这个参数产生了一个 cHash 参数,我不需要,也不想在 URL 中看到。

文档 (https://docs.typo3.org/typo3cms/extensions/core/Changelog/9.5/Feature-86365-RoutingEnhancersAndAspects.html) 说:

If the requirements are too loose, a URL signature parameter ("cHash") is added to the end of the URL which cannot be removed.

还有:

If you really have the requirement to never have a cHash argument, ensure that all placeholders are having strict definitions on what could be the result of the page segment (e.g. pagination), and feel free to build custom mappers.

功能描述仅说明如何在 ext_tables.php 中注册自定义增强器 class,但未说明如何使用自己的方面映射器:-(

很高兴,但是怎么样?

解决方法很简单,但文档似乎有误。根据文档,自定义增强器应该在 ext_tables.php 中使用 $GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['CustomPlugin'] 注册。

更新

If the mapper is registered in the ext_tables.php, it only works if you are logged in to the TYPO3 BE. The mapper must seem to be registered in the ext_localconf.php. Then it works without being logged in to the BE

查看数组 $GLOBALS['TYPO3_CONF_VARS']['SYS']['routing'] 可以看到 Aspects 和 Enhancer 的注册位置:

在ext_tables.php中注册映射器:

// Custom Routing Aspects Mapper
$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['aspects']['IdentifierValueMapper'] = \VENDOR\Extension\Routing\Aspect\IdentifierValueMapper::class;

方面class:

<?php
namespace VENDOR\Extension\Routing\Aspect;

use TYPO3\CMS\Core\Routing\Aspect\StaticMappableAspectInterface;
use TYPO3\CMS\Core\Site\SiteLanguageAwareTrait;

class IdentifierValueMapper implements StaticMappableAspectInterface
{
    use SiteLanguageAwareTrait;

    /**
     * {@inheritdoc}
     */
    public function generate(string $value): ?string
    {
        ...
        return $value !== false ? (string)$value : null;
    }

    /**
     * {@inheritdoc}
     */
    public function resolve(string $value): ?string
    {
        ...
        return isset($value) ? (string)$value : null;
    }

}

没有自定义映射器,我的 URL 总是(在我的情况下绝对 useless/only 丑陋)TYPO3 cHash 属性:

/page/2/price/asc/03510890954e251e285104f156298e55952e4c7d?cHash=dd66994f041278f4c6bf2f7f64fb09e4

现在我得到了没有 cHash 的 URL:

/page/3/price/asc/ae636e66563e72d3e4f592173f328fecbee5e44f