简体   繁体   English

TYPO3 如何处理自定义扩展的路由

[英]TYPO3 How to handle routes for custom extensions

Lat's say that I've got hypothetical ZOO extension, which has just Animal model, and Showroom plugin for displaying simple list and show actions. Lat 说我有假设的ZOO扩展,它只有Animal model,以及用于显示简单listshow操作的Showroom插件。 Very basic case so far.到目前为止非常基本的情况。

Animal model contains just two fields name which is supposed to be URI part and color ... just color. Animal model 只包含两个字段name ,应该是 URI 部分和color ……只是颜色。

SQL for it is just SQL 因为它只是

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

For linking to the single view ( show action) we can use simple URI w/out route enhancer:为了链接到单个视图( show操作),我们可以使用没有路由增强器的简单 URI:

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

Next, to add routing for our extension we'd like to add such enhancer within typo3conf/sites/yoursite/config.yaml接下来,要为我们的扩展添加路由,我们想在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

which results with a link like http://domain.tld/zoo/Elephant结果是像http://domain.tld/zoo/Elephant这样的链接

so far, so good, anyway we meet several problems here:到目前为止,一切都很好,无论如何我们在这里遇到了几个问题:

  • URI isn't lowercase URI 不是小写的
  • it doesn't replace spaces so if we have an animal with Ultra Weird Fish name the URI will be http://domain.tld/zoo/Ultra%20Weird%20Fish instead of http://domain.tld/zoo/ultra-weird-fish它不会替换空格,因此如果我们有一个动物名称为Ultra Weird Fish ,则 URI 将是http://domain.tld/zoo/Ultra%20Weird%20Fish而不是http://domain.tld/zoo/ultra-weird-fish
  • it doesn't care about uniqueness, so when we'll add several animals ie with Fish name, but different color it will create the same URI for all of them: http://domain.tld/zoo/Ultra%20Weird%20Fish它不关心唯一性,所以当我们添加几个动物时,即带有鱼名但颜色不同的动物,它将为所有动物创建相同的 URI: http://domain.tld/zoo/Ultra%20Weird%20Fish

How to properly handle this scenario?如何正确处理这种情况?

In this case, we should use additional field in our model/table, for an example slug like in pages TCA.在这种情况下,我们应该在模型/表中使用附加字段,例如 TCA pages中的示例slug The first thing to do is adding it to our SQL in typo3conf/ext/zoo/ext_tables.sql首先要做的是在typo3conf/ext/zoo/ext_tables.sql它添加到我们的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
);

then we need to add config for the new field if TCA of our table typo3conf/ext/zoo/Configuration/TCA/tx_zoo_domain_model_animal.php如果我们的表typo3conf/ext/zoo/Configuration/TCA/tx_zoo_domain_model_animal.php的TCA,我们需要为新字段添加配置

<?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' => ''
            ]
        ],

    ],
];

and finally edit our route enhancer to use slug instead of the name within typo3conf/sites/yoursite/config.yaml :最后编辑我们的路由增强器以使用slug代替typo3conf/sites/yoursite/config.yaml中的name

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

Thanks to this approach slug field will be handled properly during form editing in backend:由于这种方法slug字段将在后端的表单编辑期间得到正确处理:

TCA 中的蛞蝓类型

ProTip As usual, after each change in your code, especially in config.yaml DO NOT FORGET to clear all your caches million times:D与往常一样,每次更改代码后,尤其是在config.yaml config.yaml不要忘记清除所有缓存数百万次:D

In addition to questions in comments除了评论中的问题

Using number suffix for non-unique URI's is standard behaviour for routing (or URL rewriting in general) in TYPO3 for really long time.对非唯一 URI 使用数字后缀是很长一段时间内 TYPO3 中路由的标准行为(或一般的 URL 重写)。 Actually using added slug field allows you to enter custom slug for each item, instead of having elephant-1 and elephant-2 .实际上,使用添加的slug字段可以让您为每个项目输入自定义 slug,而不是有elephant-1elephant-2

Optionally you can also modify the TCA for the slug field to combine more fields from DB like without need for manual slug editing:或者,您还可以修改 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' => ''
    ]
],

Which automatically create slugs basing on the value of color field, like:它会根据color字段的值自动创建 slug,例如:

BE preview: BE预览:

蛞蝓的组合柱

appearance config in the TCA TCA 中的外观配置

Since TYPO3 ver.: 10.x it's possible to add a custom prefix to the slug field using class presented in documentation it will just add the prefix similar to this one you can see at translated pages/records.由于 TYPO3 版本: 10.x ,可以使用文档中提供的 class 向slug 字段添加自定义前缀,它只会添加类似于您可以在翻译页面/记录中看到的前缀。

蛞蝓的前缀

Actually it inherits two arguments, $parameters and $reference to TYPO3\CMS\Backend\Form\FormDataProvider\TcaSlug so it can be used ie for adding language part of the route, however, it's already done, so I don't find at the moment to much other usages.实际上它继承了两个 arguments, $parameters$referenceTYPO3\CMS\Backend\Form\FormDataProvider\TcaSlug所以它可以用于添加语言部分的路线,但是,它已经完成了,所以我没有找到时刻到许多其他用途。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM