Search code examples
typo3url-routingtypo3-9.x

TYPO3 routing and aspect - remove cHash from URL


With TYPO3 9.5 I try to manage the URLs from my extension with a custom aspect. The url-generation works but there is a cHash appended. First, I don't want this cHash (it's unnecessary here) and second the cHash breaks the functionality of the link (I get a cHash-comparsion-error).

I read https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/9.5/Feature-86365-RoutingEnhancersAndAspects.html#impact another time about the dynamic parameters and cHash. To remove cHash I should add aspects for all placeholders. I have just one placeholder and an aspect for this, but cHash is present.

raw-url looks like this: &tx_psoabilling_pi1[action]=showband&tx_psoabilling_pi1[controller]=Band&tx_psoabilling_pi1[band]=564&cHash=jkg24hwek8ufhqwezweklfzh

it's rendered to something like this: 2019-thisisaname?cHash=o28z3hkwejghweuhzlk

Here the part from config.yaml:

 PsoabillingPlugin:
    type: Extbase
    extension: Psoabilling
    plugin: Pi1
    routes:
      - routePath: '/{yearandbandname}'
        _controller: 'Band::showband'
        _arguments: 
          yearandbandname: band
    defaultController: 'Band::listyear'
    aspects:
      yearandbandname:
        type: BandAndYearMapper

BandAndYearMapper.php

<?php

namespace EnzephaloN\ThemePsoa\Routing\Aspect;

use TYPO3\CMS\Core\Routing\Aspect\PersistedMappableAspectInterface;
use TYPO3\CMS\Core\Site\SiteLanguageAwareTrait;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Database\ConnectionPool;

class BandAndYearMapper implements PersistedMappableAspectInterface{

    use SiteLanguageAwareTrait;

    /**
     * @param string $value
     * @return string|null
     */
    public function generate(string $value): ?string{
        if($uid=intval($value)){
            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_psoabilling_domain_model_band');
            $statement = $queryBuilder
               ->select('b.uid', 'b.name', 'y.year')
               ->from('tx_psoabilling_domain_model_band','b')
               ->leftJoin('b', 'tx_psoabilling_domain_model_year', 'y', 'b.year = y.uid')
               ->where(
                  $queryBuilder->expr()->eq('b.uid', $queryBuilder->createNamedParameter($uid))
               )
               ->execute();
            if($record = $statement->fetch()){
                if(is_array($record)){
                    return $record['year']. "-" .str_replace(" ","-",trim(strtolower($record['name'])));
                }
            }
        }
        return null;
    }

    /**
     * @param string $value
     * @return string|null
     */
    public function resolve(string $value): ?string{
        $year = substr($value, 0, 4);
        $name = str_replace("-", " ", substr($value, 5));
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_psoabilling_domain_model_band');
        $statement = $queryBuilder
            ->select('b.uid')
            ->from('tx_psoabilling_domain_model_band','b')
            ->leftJoin('b', 'tx_psoabilling_domain_model_year', 'y', 'b.year = y.uid')
            ->where(
                $queryBuilder->expr()->andX(
                    $queryBuilder->expr()->eq('y.year', $queryBuilder->createNamedParameter($year)),
                    $queryBuilder->expr()->orX(
                        $queryBuilder->expr()->like('b.name', $queryBuilder->createNamedParameter($name)),
                        $queryBuilder->expr()->like('b.name', $queryBuilder->createNamedParameter(substr($value, 5))), // if there was a - inside the bandname
                        $queryBuilder->expr()->like('b.name', $queryBuilder->createNamedParameter(substr($value, 5, strpos($name, " ")-1).'%')) // just find by beginning
                    )
                )
            )
            ->execute();
        if($record = $statement->fetch()){
            return (string)$record['uid'];
        }
        return null;
    }
}

How can the cHash be removed??


Solution

  • Like Susi wrote, switching to StaticMappableAspectInterface was the solution. The class now starts with:

    <?php
    
    namespace EnzephaloN\ThemePsoa\Routing\Aspect;
    
    use TYPO3\CMS\Core\Routing\Aspect\StaticMappableAspectInterface;
    use TYPO3\CMS\Core\Site\SiteLanguageAwareTrait;
    use TYPO3\CMS\Core\Utility\GeneralUtility;
    use TYPO3\CMS\Core\Database\ConnectionPool;
    
    class BandAndYearMapper implements StaticMappableAspectInterface{