TYPO3 9:为新闻扩展的 URL 路由增强器添加日期 [英] TYPO3 9: add date to URL routing enhancers for news extension

查看:43
本文介绍了TYPO3 9:为新闻扩展的 URL 路由增强器添加日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 tx_news 记录的详细页面以及我自己编写的日历扩展,我希望在 URL 中记录日期,直到 TYPO3 8LTS 和 realURL 扩展:/path-to/my-page/yyyy/mm/dd/extension-record-path-segment/.我设法创建了链接但附加了 cHash.

For the detail pages of tx_news records as well as a calendar extension I wrote myself I want the record date in the URL as I had it until TYPO3 8LTS with the realURL extension: /path-to/my-page/yyyy/mm/dd/extension-record-path-segment/. I managed to have the link created but with the cHash attached.

我在/typo3conf/sites/my-site/config.yaml中tx_news_pi1的routeEnhancers设置如下:

My routeEnhancers settings for the tx_news_pi1 in /typo3conf/sites/my-site/config.yaml are as follows:

routeEnhancers:
  NewsPlugin:
    type: Extbase
    limitToPages: [7]
    extension: News
    plugin: Pi1
    routes:
      - { routePath: '/{year}/{month}/{day}/{news}', _controller: 'News::detail' }
    defaultController: 'News::detail'
    requirements:
      year: '^20[0-9]{2}$'
      month: '^[01][0-9]$'
      day: '^[0-3][0-9]$'
    aspects:
      news:
        type: PersistedAliasMapper
        tableName: 'tx_news_domain_model_news'
        routeFieldName: 'path_segment'

我使用非常严格的正则表达式添加了 requirements 部分,因为 T3 变更日志中的描述 提到了这一点,以避免 cHash.

I added the requirements section with quite strict regular expressions because the description in the T3 changelog mentions this as needed in order to avoid the cHash.

我还尝试在 aspects 部分中使用 StaticRangeMappers 表示年、月和日 (如本答案中所述),但这会导致 PersistedAliasMapper 被忽略,而是显示新闻记录的 UID.或者有时甚至在 TYPO3 异常中(1/1) #1537696772 OverflowException:所有映射器的可能范围大于 10000 个项目 (当我将它们按月和日删除并将年的范围设置为仅时,它甚至这样做了2016-2019 年).

I also tried using StaticRangeMappers in the aspects section for year, month, and day (as described in this answer) but that results in the PersistedAliasMapper being ignored and showing the UID of the news record instead. Or sometimes even in a TYPO3 exception (1/1) #1537696772 OverflowException: Possible range of all mappers is larger than 10000 items (it even did so when I removed them for month and day and set the range for year to only 2016–2019).

      year:
        type: StaticRangeMapper
        start: '2016'
        end: '2100'
      month:
        type: StaticRangeMapper
        start: '01'
        end: '12'
      day:
        type: StaticRangeMapper
        start: '01'
        end: '31'
      event:
        type: PersistedAliasMapper
        tableName: 'tx_thesimplecal_domain_model_events'
        routeFieldName: 'path_segment'

我已将这篇文章更新得更短,因为一些最初提到的错误神奇地消失了.

推荐答案

这个问题的原因是月份作为带有前导零的字符串出现,但 StaticRangeMapper 构建的范围没有前导 0 并且不会像 01 那样映射月份. 一旦一个值无法映射,映射就会停止,现在新闻记录的 uid 没有映射,因为这会发生在月份的映射之后.

The reason for this problem is that the month comes as string with a leading zero but the StaticRangeMapper buildes the range without leading 0s and does not map a month like 01. The mapping stops as soon one value cannot be mapped and now the uid of the news record is not mapped since this would happen after the mapping of the month.

一个简单的解决方案是编写一个StaticMonthMapper

A simple solution is to write a StaticMonthMapper

class StaticMonthMapper implements StaticMappableAspectInterface, \Countable
{
/**
 * @var array
 */
protected $settings;


/**
 * @param array $settings
 * @throws \InvalidArgumentException
 */
public function __construct(array $settings)
{
    $this->settings = $settings;
}

/**
 * {@inheritdoc}
 */
public function count(): int
{
    return 12;
}

/**
 * {@inheritdoc}
 */
public function generate(string $value): ?string
{
    return $this->respondWhenInRange($value);
}

/**
 * {@inheritdoc}
 */
public function resolve(string $value): ?string
{
    return $this->respondWhenInRange($value);
}

/**
 * @param string $value
 * @return string|null
 */
protected function respondWhenInRange(string $value): ?string
{
    switch ($value) {
        case '01':
        case '02':
        case '03':
        case '04':
        case '05':
        case '06':
        case '07':
        case '08':
        case '09':
        case '10':
        case '11':
        case '12':
            return $value;
        default:
            return null;
    }
}
}

并注册

$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['aspects']['StaticMonthMapper'] = \Package\Namespace\StaticMonthMapper::class;

这可以在站点配置中使用.对于 days 参数,可以创建类似的映射器.

This can be used in the site config. For the days parameter a similar mapper can be created.

year:
    type: StaticRangeMapper
    start: '2016'
    end: '2100'
month:
    type: StaticMonthMapper

这篇关于TYPO3 9:为新闻扩展的 URL 路由增强器添加日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆