1

假设我有一个假设的ZOO扩展,它只有动物模型,以及用于显示简单和动作的Showroom插件。到目前为止非常基本的情况。listshow

动物模型只包含两个字段name,它们应该是 URI 部分和color......只是颜色。

SQL 只是

CREATE TABLE tx_zoo_domain_model_animal (
    name varchar(255) DEFAULT '' NOT NULL,
    color varchar(255) DEFAULT '' NOT NULL,
);

为了链接到单个视图(show操作),我们可以使用没有路由增强器的简单 URI:

/zoo?tx_zoo_showroom[action]=show&tx_zoo_showroom[animal]=123&tx_zoo_showroom[controller]=Animal&cHash=17c7b0009b50eaf0222fe66d9f

接下来,要为我们的扩展添加路由,我们想在其中添加这样的增强器typo3conf/sites/yoursite/config.yaml

routeEnhancers:
  Zoo:
    type: Extbase
    limitToPages:
      - 107  # it's always good idea to limit enhancers only to pages containing plugin
    extension: Zoo
    plugin: Showroom
    routes:
      - routePath: '/'
        _controller: 'Animal::list'
      - routePath: '/{animal-name}'
        _controller: 'Animal::show'
        _arguments:
          animal-name: animal
    aspects:
      animal-name:
        type: PersistedAliasMapper
        tableName: tx_zoo_domain_model_animal
        routeFieldName: name

结果是一个链接http://domain.tld/zoo/Elephant

到目前为止,一切都很好,无论如何我们在这里遇到了几个问题:

  • URI 不是小写的
  • 它不会替换空格,所以如果我们有一只动物名称为Ultra Weird Fish,那么 URI 将http://domain.tld/zoo/Ultra%20Weird%20Fish代替http://domain.tld/zoo/ultra-weird-fish
  • 它不关心唯一性,所以当我们添加几个动物时,即带有鱼的名字,但颜色不同,它将为所有动物创建相同的 URI:http://domain.tld/zoo/Ultra%20Weird%20Fish

如何正确处理这种情况?

4

1 回答 1

3

在这种情况下,我们应该在模型/表中使用附加字段,例如slugpagesTCA 中。首先要做的就是将它添加到我们的 SQL 中typo3conf/ext/zoo/ext_tables.sql

CREATE TABLE tx_zoo_domain_model_animal (
    name varchar(255) DEFAULT '' NOT NULL,
    color varchar(255) DEFAULT '' NOT NULL,
    slug varchar(2048), -- quite large value, but your name/slug may be loooong
);

如果我们表的 TCA,我们需要为新字段添加配置typo3conf/ext/zoo/Configuration/TCA/tx_zoo_domain_model_animal.php

<?php
return [
    'ctrl' => [...],
    'interface' => [
        // add slug to showRecordFieldList
        'showRecordFieldList' => 'sys_language_uid, l10n_parent, l10n_diffsource, hidden, name, slug, color',
    ],
    'types' => [
        // add slugto showitem
        '1' => ['showitem' => 'sys_language_uid, l10n_parent, l10n_diffsource, hidden, name, slug, color, --div--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:tabs.access, starttime, endtime'],
    ],
    'columns' => [
        'sys_language_uid' => [...],
        'l10n_parent' => [...],
        'l10n_diffsource' => [...],
        't3ver_label' => [...],
        'hidden' => [...],
        'starttime' => [...],
        'endtime' => [...],

        'name' => [...],
        'color' => [...],
        // add config for slug
        'slug' => [
            'exclude' => true,
            'label' => 'Slug',
            'displayCond' => 'VERSION:IS:false',
            'config' => [
                'type' => 'slug',
                'size' => 50,
                'generatorOptions' => [
                    'fields' => ['name'],
                    'replacements' => [
                        '/' => '-'
                    ],
                ],
                'fallbackCharacter' => '-',
                'eval' => 'uniqueInSite', // optionaly 'unique' can be used to make sure it's unique within whole TYPO3 instance.
                'default' => ''
            ]
        ],

    ],
];

最后编辑我们的路由增强器以使用slug而不是nameinside typo3conf/sites/yoursite/config.yaml

routeEnhancers:
  Zoo:
    type: Extbase
    limitToPages:
      - 107  # it's always good idea to limit enhancers only to pages containing plugin
    extension: Zoo
    plugin: Showroom
    routes:
      - routePath: '/'
        _controller: 'Animal::list'
      - routePath: '/{animal-name}'
        _controller: 'Animal::show'
        _arguments:
          animal-name: animal
    aspects:
      animal-name:
        type: PersistedAliasMapper
        tableName: tx_zoo_domain_model_animal
        routeFieldName: slug

由于这种方法slug字段将在后端的表单编辑期间得到正确处理:

TCA 中的蛞蝓类型

ProTip像往常一样,每次更改代码后,尤其是 config.yaml 不要忘记清除所有缓存数百万次:D

除了评论中的问题

对非唯一 URI 使用数字后缀是很长一段时间内 TYPO3 中路由(或一般的 URL 重写)的标准行为。实际上,使用添加slug的字段允许您为每个项目输入自定义 slug,而不是使用elephant-1and elephant-2

或者,您还可以修改 slug 字段的 TCA 以组合 DB 中的更多字段,而无需手动编辑 slug:

'slug' => [
    'exclude' => true,
    'label' => 'Slug',
    'displayCond' => 'VERSION:IS:false',
    'config' => [
        'type' => 'slug',
        'size' => 50,
        'generatorOptions' => [
            'fields' => ['name', 'color'], // combine more fields
            'fieldSeparator' => '/', // or '-' if you want slug like 'elephant-cyan' instead of `elephant/cyan`
            'replacements' => [
                '/' => '-'
            ],
        ],
        'fallbackCharacter' => '-',
        'eval' => 'uniqueInSite', // optional 'unique' can be used
        'default' => ''
    ]
],

它会根据字段的值自动创建 slug color,例如:

BE预览:

蛞蝓的组合柱

TCA 中的外观配置

由于 TYPO3 版本:可以使用文档10.x中提供的类向 slug 字段添加自定义前缀, 它只会添加类似于您可以在翻译的页面/记录中看到的前缀。

蛞蝓的前缀

实际上它继承了两个参数,$parameters因此它可以用于添加路由的语言部分,但是,它已经完成了,所以我目前没有找到很多其他用法$referenceTYPO3\CMS\Backend\Form\FormDataProvider\TcaSlug

于 2020-07-19T16:30:58.297 回答