BeforeRedirectMatchDomainEvent

New in version 12.3.

The PSR-14 event \TYPO3\CMS\Redirects\Event\BeforeRedirectMatchDomainEvent allows extensions to implement a custom redirect matching upon the loaded redirects or return the matched redirect record from other sources.

Note

The full sys_redirect record must be set using the setMatchedRedirect() method. Otherwise the Core code would fail later, as it expects, for example, the uid of the record to set the X-Redirect-By response header. Therefore, the getMatchedRedirect() method returns null or a full sys_redirect record.

Note

The BeforeRedirectMatchDomainEvent is dispatched before cached redirects are retrieved. That means, that the event does not contain any sys_redirect records. The internal redirect cache may vanish eventually, if possible. Therefore, it is left out to avoid a longer bound state to the event by properly deprecate it.

Example

Registration of the event listener in the extension's Services.yaml:

EXT:my_extension/Configuration/Services.yaml
services:
  # Place here the default dependency injection configuration

  MyVendor\MyExtension\Redirects\EventListener\MyEventListener:
    tags:
      - name: event.listener
        identifier: 'my-extension/before-redirect-match-domain'

Read how to configure dependency injection in extensions.

The corresponding event listener class:

EXT:my_extension/Classes/Redirects/EventListener/MyEventListener.php
<?php

declare(strict_types=1);

namespace MyVendor\MyExtension\Redirects\EventListener;

use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Redirects\Event\BeforeRedirectMatchDomainEvent;

final class MyEventListener
{
    public function __invoke(BeforeRedirectMatchDomainEvent $event): void
    {
        $matchedRedirectRecord = $this->customRedirectMatching($event);
        if ($matchedRedirectRecord !== null) {
            $event->setMatchedRedirect($matchedRedirectRecord);
        }
    }

    private function customRedirectMatching(BeforeRedirectMatchDomainEvent $event): ?array
    {
        // @todo Implement custom redirect record loading and matching. If
        //       a redirect based on custom logic is determined, return the
        //       :sql:`sys_redirect` tables conform redirect record.

        // Note: Below is simplified example code with no real value.
        $record = BackendUtility::getRecord('sys_redirect', 123);

        // Do custom matching logic against the record and return matched
        // record - if there is one.
        if ($record /* && custom condition against the record */) {
            return $record;
        }

        // Return null to indicate that no matched redirect could be found
        return null;
    }
}

API

class TYPO3\CMS\Redirects\Event\BeforeRedirectMatchDomainEvent

This event is fired in TYPO3CMSRedirectsServiceRedirectService->matchRedirect() for checked host and wildcard host "*".

It can be used to implement a custom match method, returning a matchedRedirect record with eventually enriched record data.

getDomain()
Return type

string

Returns

Request domain name (host)

getPath()
Return type

string

Returns

Request path

getQuery()
Return type

string

Returns

Request query parameters

getMatchDomainName()
Return type

string

Returns

Domain name which should be checked, and getRedirects() items are provided for

getMatchedRedirect()
Return type

array

Returns

Returns the matched sys_redirect record or null

setMatchedRedirect(array $matchedRedirect)
Parameters
  • $matchedRedirect (array) -- Set matched sys_redirect record or null to clear prior set record