How to create a page url from page uid

user1455455 picture user1455455 · Jan 6, 2015 · Viewed 11.3k times · Source

I am working on a typo3 extension and I want to generate an url from the page id. Currently I create the url by appending index.php?id=ID to $GLOBALS['TSFE']->baseURL.

Is there any other way to create a readable url from the page id, and if yes, how it can be done?

Answer

lorenz picture lorenz · Jan 6, 2015

Since Extbase controllers have an UriBuilder object, you should use it:

$uri = $this->uriBuilder->reset()
    ->setTargetPageUid($pageUid)
    ->setCreateAbsoluteUri(TRUE)
    ->build();

You can also set an array of arguments if you need to:

$arguments = array(
    array('tx_myext_myplugin' =>
        array(
            'article' => $articleUid,
        )
    )
);

Or, if you don't need an extension prefix:

$arguments = array(
    'logintype' => 'login'
);

(Of course you can mix the two variants.)

And then use:

$uri = $this->uriBuilder->reset()
    ->setTargetPageUid($pageUid)
    ->setCreateAbsoluteUri(TRUE)
    ->setArguments($arguments)
    ->build();