How to create an internal URI in TYPO3

Sometimes you want to generate TYPO3 internal URIs programmatically. Now you could try to create the URL manually on your own, or use the UriBuilder class. This handy tool is pretty neat to create any kind of internal URI. In a controller this class is available by $this->getControllerContext()->getUriBuilder(). If you want to use the UriBuilder somewhere else, you need to load an instance of the class by the ObjectManager.

$objectManager = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager\ObjectManager::class);
$uriBuilder = $objectManager->get(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class);

When you have access to an instance of the builder, it's pretty straight forward from now on. The class has a bunch of simple setters, to control the generation of the URI. Best practice is to reset all the options of the builder on start of every new URI creation.

$uri = $uriBuilder->reset()->setTargetPageUid(1);

Personally I use this class mostly to create URIs for controller-actions of a plugin. So beside a pid I would need to tell which plugin, controller and action I want to address.

// array of additional get parameters
// will become something like: &foo=bar
$getArgs = [
  'foo' => 'bar',

// array of plugin arguments
// will become something like: &tx_extension_plugin['foo']=bar
$pluginArgs = [
  'foo' => 'bar',

$uri = $uriBuilder
  ->uriFor('action', $pluginArgs, 'Controller', 'extension', 'plugin');

The typolink view helper uses the builder class too, to generate any kind of URLs. It's easy, fast and works great with RealUrl.

Create Frontend URI in Backend

The above approach does only work well in frontend context. If you want to create a frontend link in TYPO3 backend, you need to do a bit more. It's necessary to initialize a TSFE instance before you can create frontend links with the help of typolink there too.

use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\TimeTracker\TimeTracker;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

// page id of the target page
$targetPageId = 1;

// initialize time tracker
if (!is_object($GLOBALS['TT'])) {
    $GLOBALS['TT'] = new TimeTracker();

// initialize TSFE
if (!is_object($GLOBALS['TSFE'])) {
    $GLOBALS['TSFE'] = GeneralUtility::makeInstance(TypoScriptFrontendController::class, $GLOBALS['TYPO3_CONF_VARS'], $targetPageId, 0);

// make it work with realurl too
if (ExtensionManagementUtility::isLoaded('realurl')) {
    $rootLine = BackendUtility::BEgetRootLine($targetPageId);
    $host = BackendUtility::firstDomainRecord($rootLine);
    $_SERVER['HTTP_HOST'] = $host;

// all arguments
$args = [
    'foo' => 'bar'
    'tx_extension_plugin[foo]' => 'bar',
    'tx_extension_plugin[action]' => 'action',
    'tx_extension_plugin[controller]' => 'Controller',

// create uri by typolink helper
$cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
$uri = $cObj->typolink_URL([
    'parameter' => $targetPageId,
    'linkAccessRestrictedPages' => 1,
    'additionalParams' => GeneralUtility::implodeArrayForUrl(NULL, $args),

Einen Kommentar verfassen

Ihre E-Mail-Adresse wird nicht veröffentlicht, sie dient nur der Identifikation! Für die Profilbilder wird Gravatar verwendet und reCAPTCHA, um Spam zu vermeiden.

Kommentare 0

Es sind noch keine Kommentare für diesen Beitrag vorhanden.