Optional argument in Extbase RouteEnhancer

Mikel Wohlschlegel :

I need to add an optional argument to the url of news records (own extension).

Current enhancer configuration:

MyNewsSystem:
type: Extbase
extension: MyNewsSystem
plugin: News
routes:
  -
    _controller: 'News::show'
    routePath: '/detail/{news_title}'
    _arguments:
      news_title: selectedNewsItem
aspects:
  news_title:
    type: PersistedAliasMapper
    tableName: tx_mynewssystem_domain_model_news
    routeFieldName: path_segment

This works fine and generates the following url scheme: /detail/title-of-news/

Now I need to add an optional parameter to the end of the url: /detail/title-of-news/?hl=String+with+info Adding this parameter will end up in 404.

Is there a possibilty to just ignore that argument in my routeenhancer? Or how do I proceed?

Jonas Eberle :

You are getting a 404 Not Found because a cHash-error occured. If you generate the link through TYPO3 (e.g. with f:link or typolink) and your routing configuration is correct, a correct ?cHash=-parameter will be appended.

If your custom parameter does not change the HTML output at all you can add it to the [FE][cacheHash][excludedParameters] (in "Installation-wide settings"). Mind that this means that different values of this parameter would all go onto the same cache entry. Thus, if you need to do anything with the parameter, your only option is JavaScript.

The other case is quite common, but I'll add all the conditions for that case:

If you need to alter the HTML output depending on that parameter and ( you absolutely do not want a ?cHash or you cannot have TYPO3 generate that link ) and you cannot use the PersistedAliasMapper (because the parameter is free text and not bound to anything in the database) and you cannot use the other aspect mappers (like StaticRangeMapper), your only option is to write a custom Aspect.

Have a look here https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/Routing/ExtendingRouting.html - in your case I would look for inspiration in \TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper. It is advisable to narrow down the possible values as much as possible (to prevent cache flooding, the reason why cHash was added to TYPO3), but you will be able to get rid of the ?cHash.


Just for completeness: this is how it "should" be. There are currently some inconsistencies with cached vs. non-cached and "route-enhanced" pages (like your detail page) vs. "pagetree" pages whether or not a cHash-error is thrown. But if you treat parameters like suggested here, I would think you are safe for the future.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=220684&siteId=1