TYPO3 v11: How to remove controller and hash from route

75 views Asked by At

Paths from a custom TYPO3 extensions look like this:

https://example.com/events/detail/event-title/test-event/?tx_eventmanager_eventdisplay%5Bcontroller%5D=Event&cHash=f32fdcaf3d016b8b89861dfab5a0558d

And I want them to look like: https://example.com/events/detail/event-title/test-event/

routeEnhancer configuration looks like this:

routeEnhancers:
  EventDetail:
    type: Plugin
    routePath: '/event-title/{event}'
    namespace: tx_eventmanager_eventdisplay
    defaults:
      controller: 'Event'
    aspects:
      event:
        type: PersistedAliasMapper
        tableName: tx_eventmanager_domain_model_event
        routeFieldName: path_segment

TYPO3 version is 11.5.34

The exact same configuration produces the desired result in another extension (in another Site) from which the current one was forked years ago. It worked even without "defaults", which I added because it looked like they might help. I tried it with type: Extbase instead but that didn't work with either extension for some reason.

So there seems to be some small but crucial difference between both extensions (or environments), but both have diverged considerably - what should I look out for? Or can I fix it by some additional configuration in routeEnhancer?

I already tried narrowing it down with ChatGPT, but that didn't help much. I tried different routeEnhancer configurations that it offered, which didn't improve it. And based on its advice I compared:

  • field "path_segment" in DB and indexes,
  • 'path_segment' in TCA
  • "configurePlugin" block in ext_localconf.php

All of which were identical in both extensions. Other than that its advice is very broad.

1

There are 1 answers

4
Stefan Bürk On

It seems, that you are talking about a Extbase Plugin and want to replace the extbase request get arguments with a speaking url.

Using the site configuraiton to define a routing is a good start, and the right place to do so. However, you are using the "wrong" route enhancer. The culprit is, that TYPO3 knowns two types of plugins - AbstractPlugins (Pi42 based/legacy) and Extbase Plugins - and you need the correct route enhancer for each plugin type.

The documentation explains the Extbase Plugin Route-Enhancer [1] and you may want to read it up for further explanation, special for the aspect part and posibilities.

A rough start should work using following route configuration:

routeEnhancers:
  EventDetail:
    type: Extbase
    extension: Eventmanager
    plugin: EventDisplay
    routes:
      - routePath: '/event-title/{event}'
        _controller: 'Event::show'
        _arguments:
          event: 'event'
    aspects:
      event:
        type: PersistedAliasMapper
        tableName: tx_eventmanager_domain_model_event
        routeFieldName: path_segment

Note: Always use a pre-fix for the route path and avoid recuring/same prefix for multiple plugins - or use limitToPage which helps as long as not two plugings with same prefix are on the same page. And you should use routePath prefix which are unlikely to be the slug of children page if tree lige slug paths are used (default).

It's not really recommended to use the Plugin routeenhanver for a extbase plugin as it takes care of Extbase specific requirements.

Other options

If it is really not a extbase plugin, and you want to use the type Plugin enhancer than there are other culprits. First, that type does not support aspects argument mapping.

So maybe following works for you:

routeEnhancers:
  EventDetail:
    type: Plugin
    routePath: '/event-title/{event}'
    namespace: tx_eventmanager_eventdisplay
    requirements:
      event: '[a-zA-Z0-9]+'
    aspects:
      event:
        type: PersistedAliasMapper
        tableName: tx_eventmanager_domain_model_event
        routeFieldName: path_segment

The namespace (full plugin name) and the should be not a duplicate of the other plugin and unique - and matching the extension/plugin named used for registration. See [2] for description if this type.