首页 > 解决方案 > TYPO3 9.5 LTS routeEnhancers for news with uid 使路径独一无二

问题描述

您好,我在 TYPO3 Slack 频道上发现以下内容是一个人正在使用的内容。这个片段yaml存在于/config/my-websitename/config.yaml...

routeEnhancers:
  NewsList:
    type: Extbase
    limitToPages: [2,20,21,22,92]
    extension: News
    plugin: Pi1
    routes:
      - routePath: '/p{page}'
        _controller: 'News::list'
        _arguments: {'page': '@widget_0/currentPage'}
      - routePath: '/{news_title}'
        _controller: 'News::detail'
        _arguments: {'news_title': 'news'}
    defaultController: 'News::list'
    defaults:
      page: '0'
    requirements:
      page: '\d+'
      news_title: '^[a-zA-Z0-9].*$'
    aspects:
      page:
        type: StaticRangeMapper
        start: '1'
        end: '100'
      news_title:
        type: PersistedAliasMapper
        tableName: 'tx_news_domain_model_news'
        routeFieldName: 'path_segment'

问题

但我注意到,当我有多篇标题相同的文章时,没有唯一的 URL。

问题

如何将文章添加uid到路径以使其独一无二,或者这是一个好主意?我发现它在文档中被忽略了,但不知道如何让它发挥作用来扩展我已经拥有的东西,或者如果有人可以给我一个更好的例子来说明如何获取新闻的唯一网址?

https://docs.typo3.org/typo3cms/extensions/core/latest/Changelog/9.5/Feature-86365-RoutingEnhancersAndAspects.html#persistedpatternmapper

标签: typo3typo3-9.x

解决方案


答案

routeEnhancers:
  NewsPlugin:
    type: Extbase
    extension: 'News'
    plugin: 'Pi1'
    routes:
      - { routePath: '/{news_title}', _controller: 'News::detail', _arguments: {'news_title': 'news'} }
    defaultController: 'News::list'
    aspects:
      news_title:
        type: PersistedPatternMapper
        tableName: 'tx_news_domain_model_news'
        routeFieldPattern: '^(?P<path_segment>.+)-(?P<uid>\d+)$'
        routeFieldResult: '{path_segment}-{uid}'

推荐阅读