如何在TYPO3 CMS 9.5 LTS中创建一个动态字符串路由增强器?

huangapple go评论80阅读模式
英文:

How to create a dynamic string route enhancer in TYPO3 CMS 9.5 LTS?

问题

我已经为我的扩展路由增强器创建了一个简单的方面,如下所示:

routeEnhancers:
  Trainee:
    type: Extbase
    extension: Dsinstitution
    plugin: Dslisttrainees
    routes:
      - routePath: '/trainee/{trainee-identifier}'
        _controller: 'Trainee::show'
        _arguments:
          trainee-identifier: trainee
    defaultController: 'Trainee::list'
    aspects:
      trainee-identifier:
        type: PersistedPatternMapper
        tableName: 'tx_dsinstitution_domain_model_trainee'
        routeFieldPattern: '^(?<lastname>.+)-(?<prename>.+)-(?<uid>\d+)$'
        routeFieldResult: '{lastname}-{prename}-{uid}'

问题是,如果有人有一个非常复杂的名字,会破坏预期的URL结构(例如,其中包含&amp;/)。 为此,扩展news 使用了path_segment 属性而不是多个字段。

为此,我已经扩展了我的ext_tables.sql 文件,添加了该属性。但是我如何强制TCA自动填充它以带有“lastname-prename-uid”的清理结构?我不理解news 扩展的方法。

英文:

I've created a simple aspect for my extension route enhancer like so:

routeEnhancers:
  Trainee:
    type: Extbase
    extension: Dsinstitution
    plugin: Dslisttrainees
    routes:
      - routePath: &#39;/trainee/{trainee-identifier}&#39;
        _controller: &#39;Trainee::show&#39;
        _arguments:
          trainee-identifier: trainee
    defaultController: &#39;Trainee::list&#39;
    aspects:
      trainee-identifier:
        type: PersistedPatternMapper
        tableName: &#39;tx_dsinstitution_domain_model_trainee&#39;
        routeFieldPattern: &#39;^(?&lt;lastname&gt;.+)-(?&lt;prename&gt;.+)-(?&lt;uid&gt;\d+)$&#39;
        routeFieldResult: &#39;{lastname}-{prename}-{uid}&#39;

The problem is if there is someone with a very cryptic name which would destroy the expected url structure (e.g. with &amp; or / in it). For that the extension news uses a path_segment attribute instead of multiple fields.

For that I've extended my ext_tables.sql with that attribute. But how can I force the TCA to auto fill it with the sanitized structure of "lastname-prename-uid"? I don't understand the news extension way.

答案1

得分: 0

Answer: 不要!根据文档和一些开发者的建议,你不应该在持久化模式映射器中使用自由文本字段。相反,你可以在 TCA 中使用 slug。详细信息请查看文档:https://docs.typo3.org/m/typo3/reference-tca/master/en-us/ColumnsConfig/Type/Slug.html

在你的模型的 TCA 中添加类似以下内容:

'urlslug' => [
    'exclude' => true,
    'label' => 'urlslug',
    'config' => [
        'type' => 'slug',
        'generatorOptions' => [
            'fields' => ['lastname', 'prename', 'uid'],
            'fieldSeparator' => '-',
            'prefixParentPageSlug' => true
        ],
        'fallbackCharacter' => '-',
        'eval' => 'uniqueInSite',
        'default' => ''
    ]
]

请记得将 urlslug 添加到你的模型中,同时也要在你的扩展的 ext_tables.sql 中添加相应的字段。另外,slug 只会在对象上生成,只有通过 TCA(后台)创建的对象才会生成 slug。

英文:

Answer: Don't!

In the documentation and according to recommendations from several developers you shouldn't use free text fields in the Persisted Pattern Mapper. Instead you use a slug for that in your TCA.

For more have a look in the documentation: https://docs.typo3.org/m/typo3/reference-tca/master/en-us/ColumnsConfig/Type/Slug.html

Adding to your TCA of your model something like:

&#39;urlslug&#39; =&gt; [
    &#39;exclude&#39; =&gt; true,
    &#39;label&#39; =&gt; &#39;urlslug&#39;,
    &#39;config&#39; =&gt; [
        &#39;type&#39; =&gt; &#39;slug&#39;,
        &#39;generatorOptions&#39; =&gt; [
            &#39;fields&#39; =&gt; [&#39;lastname&#39;, &#39;prename&#39;, &#39;uid&#39;],
            &#39;fieldSeparator&#39; =&gt; &#39;-&#39;,
            &#39;prefixParentPageSlug&#39; =&gt; true
        ],
        &#39;fallbackCharacter&#39; =&gt; &#39;-&#39;,
        &#39;eval&#39; =&gt; &#39;uniqueInSite&#39;,
        &#39;default&#39; =&gt; &#39;&#39;
    ]
]

Remember to add urlslug to your Model and to the ext_tables.sql of your extension as well. Also slugs will only be generated on new objects, only created with TCA (backend).

huangapple
  • 本文由 发表于 2020年1月6日 17:31:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609623.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定