3

I am trying to set up a RouteEnhacer for the TYPO3 extension web2pdf to rewrite URLs like https://example.com/subpage/?tx_web2pdf_pi1[action]=generatePdfLink&tx_web2pdf_pi1[argument]=printPage&tx_web2pdf_pi1[controller]=Pdf&cHash=123456789 to https://example.com/subpage/pdf/.

By using …

routeEnhancers:
  Web2pdf:
    type: Plugin
    routePath: '/pdf/{argument}'
    namespace: 'tx_web2pdf_pi1'
    aspects:
      argument:
        type: StaticValueMapper
        map:
          1: printPage

… I already got the URLs a bit shorter:

https://example.com/subpage/pdf/1/?tx_web2pdf_pi1[action]=generatePdfLink&tx_web2pdf_pi1[controller]=Pdf&cHash=123456789

Can the routing be optimized further so that I get the desired short URL?

I suspect an Extbase route enhancer would be better, but my attempts have all failed and no rewriting has taken place.

Ben
  • 811
  • 12
  • 28

2 Answers2

1

Your current solution is already pretty close. The only thing you need to do is expand it to all the aspects of the query.

The query in question contains 3 different aspects (hash is exempt from this) we need to substitute:

 1. tx_web2pdf_pi1[argument] (you already configured this one)
 2. tx_web2pdf_pi1[action]
 3. tx_web2pdf_pi1[controller]

If we continue like you started we end up with something like this:

routeEnhancers:
  Web2pdf:
    type: Plugin
    routePath: '/pdf/{argument}{action}{controller}'
    namespace: 'tx_web2pdf_pi1'
    aspects:
      argument:
        type: StaticValueMapper
        map:
          1: printPage
      action:
        type: StaticValueMapper
        map:
          2: generatePdfLink
      controller:
        type: StaticValueMapper
        map:
          3: Pdf

This configuration should generate the following link:

https://example.com/subpage/pdf/123

To get the desired output we just need to edit the configuration a little bit:

routeEnhancers:
  Web2pdf:
    type: Plugin
    routePath: '/{argument}{action}{controller}'
    namespace: 'tx_web2pdf_pi1'
    aspects:
      argument:
        type: StaticValueMapper
        map:
          p: printPage
      action:
        type: StaticValueMapper
        map:
          d: generatePdfLink
      controller:
        type: StaticValueMapper
        map:
          f: Pdf

This should yield your requested link structure:

https://example.com/subpage/pdf
Christoph Kern
  • 419
  • 2
  • 7
  • Even though I chose Mikel's approach as accepted, it is a very good solution and definitely helps a lot for future problems and understanding of routeEnhancers. Thank you very much. – Ben Jul 15 '22 at 12:58
1

IMO there is no need to map controller and action, as these are default arguments, automatically handled with a proper configured enhancer. The only (non-standard / custom) argument which needs to be mapped is tx_web2pdf_pi1[argument].

Here is the extbase route enhancer, which generates /your/subpage/pdf:

routeEnhancers:
  Pdf:
    type: Extbase
    extension: Web2pdf
    plugin: Pi1
    routes:
      - routePath: '/{your_argument_replacement}'
        _controller: 'Pdf::generatePdfLink'
        _arguments:
          your_argument_replacement: 'argument'
    aspects:
      argument:
        type: StaticValueMapper
        map:
          pdf: 'printPage'

Tested with the following link, rendered by fluid viewhelper:

<f:link.action action="generatePdfLink"
                       controller="Pdf"
                       extensionName="Web2pdf"
                       arguments="{argument:'printPage'}">PDF</f:link.action>
Mikel Wohlschlegel
  • 1,364
  • 1
  • 11
  • 23