How to create page URL from uid page

I am working on a typo3 extension and I want to generate the page url. I am currently creating a URL by adding index.php?id=ID to $GLOBALS['TSFE']->baseURL .

Is there another way to create a readable URL from the page id, and if so, how can this be done?

+7
source share
4 answers

Since Extbase controllers have a 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:

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

Or, if you do not need an extension prefix:

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

(Of course, you can mix the two options.)

And then use:

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

If you are not using the extbase controller context, you can use the standard TYPO3 functionality:

 $url = $GLOBALS['TSFE']->cObj->typoLink_URL( array( 'parameter' => $pageUid, 'forceAbsoluteUrl' => true, ) ); 
+6
source

If you did not initialize $GLOBALS['TSFE'] and if you want to avoid this error https://forge.typo3.org/issues/71361 , you should initialize $GLOBALS['TSFE'] as follows:

 if (!isset($GLOBALS['TSFE'])) { $pid = (int)GeneralUtility::_POST('pid'); $rootline = \TYPO3\CMS\Backend\Utility\BackendUtility::BEgetRootLine($pid); foreach ($rootline as $page) { if ($page['is_siteroot']) { $id = (int)$page['uid']; break; } } $type = 0; if (!is_object($GLOBALS['TT'])) { $GLOBALS['TT'] = new \TYPO3\CMS\Core\TimeTracker\NullTimeTracker; $GLOBALS['TT']->start(); } $GLOBALS['TSFE'] = GeneralUtility::makeInstance('TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController', $GLOBALS['TYPO3_CONF_VARS'], $id, $type); $GLOBALS['TSFE']->connectToDB(); $GLOBALS['TSFE']->initFEuser(); $GLOBALS['TSFE']->determineId(); $GLOBALS['TSFE']->initTemplate(); $GLOBALS['TSFE']->getConfigArray(); if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('realurl') ) { $host = \TYPO3\CMS\Backend\Utility\BackendUtility::firstDomainRecord($rootline); $_SERVER['HTTP_HOST'] = $host; } } 
0
source

Generate an external interface plugin / extension from the Backend Module with the cHash parameter

NOTE. Remember to include the lines on top of the controller

 use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Frontend\Page\CacheHashCalculator; 

 $siteUrl = GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . 'index.php?'; $query = array( 'id' => 57, // Target page id 'tx_event_eventfe' => array( 'controller' => 'Event', 'action' => 'show', 'eventId' => 15 // Record uid ) ); $cacheHasObj = GeneralUtility::makeInstance(CacheHashCalculator::class); $cacheHashArray = $cacheHasObj->getRelevantParameters(GeneralUtility::implodeArrayForUrl('', $query)); $query['cHash'] = $cacheHasObj->calculateCacheHash($cacheHashArray); $uri = $siteUrl . http_build_query($query); 
0
source

Source: https://habr.com/ru/post/980580/


All Articles