TYPO3 v9 上带有 StaticMappableAspectInterface 的自定义路由方面未执行

Custom Routing Aspect with StaticMappableAspectInterface on TYPO3 v9 not executed

我必须为我自己的 TYPO3 扩展做一个自定义路由方面,它使用来自 JSON(不在 TYPO3 数据库中)的 URL 中的参数来删除 cHash 到 URL 中SEO 目的。

所以我跟随 documentation 并在 myExtension/Classes/Routing/Aspect

中添加了一个 CustomValueMapper.php

为了测试目的,将下面的代码放入此文件中:

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

use TYPO3\CMS\Core\Routing\Aspect\StaticMappableAspectInterface;
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;

class CustomValueMapper implements StaticMappableAspectInterface
{

    /**
     * {@inheritdoc}
     */
    public function generate(string $value): ?string
    {
        DebuggerUtility::var_dump('tagadadada');
        $value = 'fr-00002032';
        return $value;
        // die($value);
        // return $value !== false ? (string)$value : null;
    }

    /**
     * {@inheritdoc}
     */
    public function resolve(string $value): ?string
    {
        DebuggerUtility::var_dump('tagadadodo');
        $value = 'fr-00002032';
        return $value;
        // die($value);
        // return isset($value) ? (string)$value : null;
    }

}

变成ext_localconf.php我加了:

$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['aspects']['MyExtensionCustomValueMapper'] = \VENDOR\MyExtension\Routing\Aspect\CustomValueMapper::class;

如果我调试我的控制器:

DebuggerUtility::var_dump($GLOBALS['TYPO3_CONF_VARS']['SYS']['routing'], 'mapper');

我可以看到我的自定义方面声明为 ext_localconf.php

aspects => array(7 items)
  LocaleModifier => 'TYPO3\CMS\Core\Routing\Aspect\LocaleModifier' (44 chars)
  PersistedAliasMapper => 'TYPO3\CMS\Core\Routing\Aspect\PersistedAliasMapper' (50 chars)
  PersistedPatternMapper => 'TYPO3\CMS\Core\Routing\Aspect\PersistedPatternMapper' (52 chars)
  StaticRangeMapper => 'TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper' (47 chars)
  StaticValueMapper => 'TYPO3\CMS\Core\Routing\Aspect\StaticValueMapper' (47 chars)
  MyExtensionCustomValueMapper => 'VENDOR\MyExtension\Routing\Aspect\CustomValueMapper' (47 chars)

进入站点配置(进入 config.yaml 文件)我添加了

...
routeEnhancers:
  MyCustomPlugin:
    type: Extbase
    extension: myextension
    plugin: pi1
    routes:
      - { routePath: '/', _controller: 'myController::search' }
      - { routePath: '{myParam}', _controller: 'myController::show', _arguments: {'myParam': '{myParam}'} }
    defaultController: 'myController::search'
    aspect:
      myParam:
         type: MyExtensionCustomValueMapper

所以URL用routePath重写,看起来像

http://localhost:8080/mytestpage/fr-00002032?cHash=11a51779bcff3bc03283087ea6ff9aa1

但我不需要 cHash 参数。

事实上,我的 CustomValueMapper.php 文件没有被读取。如果我在其中添加一个大的语法错误,什么也不会发生。没有错误显示,什么都没有。

进入站点配置(进入config.yaml文件), 如果您有多个参数发送到控制器 必须映射所有参数,否则缺少的参数将“隐藏”在 chash 参数中。 例如:

routeEnhancers:
MyCustomPlugin:
type: Extbase
extension: myextension
plugin: myplugin
routes:
  - routePath: '/'
    _controller: 'myController::search'
  - routePath: '/{myParam}'
    _controller: 'myController::show'
    _arguments: 
      myParam1: 'myParam1'
      myParam2: 'myParam2'
      myParam3: 'myParam3'  
defaultController: 'myController::search'
aspect:
  myParam1:
     type: MyExtensionCustomValueMapper
  myParam2:
     type: MyExtensionCustomValueMapper
  myParam3:
     type: MyExtensionCustomValueMapper

它可能正在工作... 最好的问候