typo3routertypo3-extensions

Route definition does not taking extension-name and plugin-name into account in TYPO3


Lets say we have 2 TYPO3 extensions, each containing an Extbase plugin and each providing a router YAML configuration:

# Route config for myext_aaa which contains the Extbase plugin "Checkout" using CheckoutController
routeEnhancers:
  MyextAaaCheckout:
    type: Extbase
    extension: myext_aaa
    plugin: Checkout
    routes:
      -
        routePath: '/{localized-order-new}/{course-name}'
        _controller: 'Checkout::newOrder'
        _arguments:
          course-name: 'course'
    aspects:
      course-name:
        type: PersistedAliasMapper
        tableName: my_course_table
        routeFieldName: slug
      localized-order-new:
        type: LocaleModifier
        default: 'order'
        localeMap:
          - locale: 'de_DE.*'
            value: 'buchen'

# Route config for myext_zzz which contains the Extbase plugin "CheckoutXxx" using CheckoutXxxController
routeEnhancers:
  MyextZzzCheckoutXxx:
    type: Extbase
    extension: mzext_zzz
    plugin: CheckoutXxx
    routes:
      -
        routePath: '/{zzz-localized-order-new}/{course-name}'
        _controller: 'CheckoutXxx::newOrder'
        _arguments:
          course-name: 'course'
    aspects:
      course-name:
        type: PersistedAliasMapper
        tableName: my_course_table
        routeFieldName: slug
      zzz-localized-order-new:
        type: LocaleModifier
        default: 'order'
        localeMap:
          - locale: 'de_DE.*'
            value: 'buchen'

Now we have 2 pages, each contain one of the plugins:

The issue

Opening the URL for page1 results in an error, because CheckoutController::orderNewAction(Course $course) is missing the $course parameter! It seems that the route config /buchen/{course-name} from "mzext_zzz" is used (or overwrites the first definition). But why? Does the router not taking extension: mzext_zzz and plugin: CheckoutXxx into account for the route definition?

Or in other words: how is it possible, that the route /buchen/{course-name} can be used by two completely differnt plugins from two different extensions?


Solution

  • how is it possible, that the route /buchen/{course-name} can be used by two completely differnt plugins from two different extensions?

    As long as only one of the conflicting plugins is on one page, you need to limit the plugin route to specific page(s) using the limitToPages option for the plugin definition (see below)

    However, that is not dynamic so you need to write it hardcoded in the SiteConfig config.yaml. Since TYPO3 v12 there are events (siteconfig load/write) which you can use to fill in the limitToPages dynamically.

    Another approach you could use, ist to dynamically write a sub-file for example on page / content element saves and collect all page id's for custom plugin and write the specific file (based on a template) - and include the generated one (clearing cache on changes).

    If i'm not fully wrong, there is a extension doing some dynamic stuff / extending core route-enhancer to define limitToPages dynamically.

    limitToPages

    An array of page IDs where this enhancer should be called. This is optional. This property (array) triggers an enhancer only for specific pages. In case of special plugin pages, it is recommended to enhance only those pages with the plugin to speed up performance of building page routes of all other pages.

    [1] https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/ApiOverview/Routing/AdvancedRoutingConfiguration.html