Skip to content

Instantly share code, notes, and snippets.

@koehnlein
Last active November 21, 2019 09:42
Show Gist options
  • Save koehnlein/4314d992cdfaeca8960a1e7042c18fb6 to your computer and use it in GitHub Desktop.
Save koehnlein/4314d992cdfaeca8960a1e7042c18fb6 to your computer and use it in GitHub Desktop.
Create and use a simple individual routing aspect "ContentElementIdAspect" to have URLs without cHash parameter. It only validates, if the given value does exist in database. Of course, you can even do some mapping and unmapping to not show the uid in the URL, but replace it with another unique value or combination.
# enable aspect in your routing configuration
routeEnhancers:
MyExtensionRouting:
# In the example, I use the Extbase routing enhancer, but the aspect should work with other enhancers, too.
type: Extbase
extension: MyExample
plugin: Foolist
routes:
- { routePath: '/fancy/{cid}/', _controller: 'Foo::list' }
aspects:
cid:
type: ContentElementIdAspect
<?php
declare(strict_types=1);
namespace Vendor\MyExample\Routing\Aspect;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Routing\Aspect\StaticMappableAspectInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class ContentElementIdAspect implements StaticMappableAspectInterface
{
public function generate(string $value): ?string
{
if ($this->isValidContentElementUid($value)) {
return $value;
}
return null;
}
public function resolve(string $value): ?string
{
if ($this->isValidContentElementUid($value)) {
return $value;
}
return null;
}
/**
* Validate, if $value is a valid uid in tt_content
*
* @param mixed $uid
* @return bool
*/
protected function isValidContentElementUid($value): bool
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content');
return (bool)$queryBuilder
->select('uid')
->from('tt_content')
->where(
$queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($value)),
$queryBuilder->expr()->eq('CType', $queryBuilder->createNamedParameter('list')),
$queryBuilder->expr()->eq('list_type', $queryBuilder->createNamedParameter('myexample_foolist'))
)
->execute()
->rowCount();
}
}
<?php
// register aspect
$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['aspects']['ContentElementIdAspect'] = \Vendor\MyExample\Routing\Aspect\ContentElementIdAspect::class;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment