如何从页面uid创建页面URL

时间:2015-01-06 10:49:31

标签: typo3 extbase

我正在开发一个typo3扩展程序,我想从页面ID生成一个url。目前,我通过将index.php?id=ID附加到$GLOBALS['TSFE']->baseURL来创建网址。

是否还有其他方法可以从页面ID创建可读的网址,如果有,可以如何处理?

4 个答案:

答案 0 :(得分:13)

由于Extbase控制器有一个UriBuilder对象,你应该使用它:

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

如果需要,您还可以设置参数数组:

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

或者,如果您不需要扩展名前缀:

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

(当然你可以混合两种变体。)

然后使用:

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

答案 1 :(得分:6)

如果您不在extbase控制器上下文中,则可以使用标准的TYPO3功能:

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

答案 2 :(得分:0)

如果您尚未初始化$GLOBALS['TSFE'],并且希望避免此错误https://forge.typo3.org/issues/71361,则必须以这种方式初始化$GLOBALS['TSFE']

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;
            }
        }

答案 3 :(得分:0)

  

使用cHash参数从后端模块生成前端插件/扩展链接

注意:请不要忘记在控制器顶部的以下行中添加

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);