带有 extbase 的 TYPO3:后端挂钩中的 UriBuilder(或类似的东西)

TYPO3 with extbase: UriBuilder (or something similar) in backend hook

我在 TYPO3(带有 extbase)中编写了一个后端挂钩,用于在设置 table 的某些值时发送电子邮件。我现在需要做的是生成一个前端 URL。但我似乎无法掌握 UriBuilder。如何在后端挂钩上下文中构建有效的前端 url?我试过的是:

$uriBuilder = $objectManager->get('\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder');
$link = $uriBuilder->setTargetPageUid($this->formPageId)->buildFrontendUri();

这会导致以下错误:

Fatal error: Call to a member function typoLink_URL() on a non-object in /data/www/path/typo3/sysext/extbase/Classes/Mvc/Web/Routing/UriBuilder.php on line 640

这到底是什么意思?我也尝试调用 $uriBuilder->initializeObject(),但错误仍然存​​在。我是不是以完全错误的方式解决这个问题,还是我遗漏了什么?

我找到了答案,但天知道这感觉太复杂了,不可能是对的。我所做的是在我的扩展中创建一个前端 ajax 调用,并在我的后端挂钩中调用 ajax 页面来获取 url:

$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('\TYPO3\CMS\Extbase\Object\ObjectManager');
$libDiv = $objectManager->get('\TYPO3\CMS\Core\Utility\GeneralUtility');
$siteUrl = 'http://domain.com/';
//now set the ajax url
$url = $siteUrl.'index.php?type=1447752540&id='.$this->ajaxPageId;
//set headers because otherwise it doesn't work
$headers = array(
    'Cookie: fe_typo_user=' . $_COOKIE['fe_typo_user']
);
//as far as I understand it, this is the equivalent of file_get_contents
$result = $libDiv::getURL($url, false, $headers);

我的插件只调用 uribuilder 并生成所需的 link。

...我觉得好脏

什么错别字版本?

尝试使用"build()"代替"buildFrontendUri()",让typo3识别环境。 如果您在 extbase 控制器中,则不需要 "create" uribuilder

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

我创建了一个 FrontendUriBuilderClass,您可以使用它创建一个 Frontend-Link 到带有参数等的特定操作。

有了这个助手 class 就可以在后端创建一个 Typo3 前端 link。放心使用吧!

/**
 * Class FrontendUriBuilder for creating a frontend link in the backend
 * @package PDVSysteme\Pdvsupportbase\Util
 */
class FrontendUriBuilder
{

private $pageId = 1;

private $extensionName = null;

private $pluginName = null;

private $actionName = null;

private $controllerName = null;

private $arguments = null;

private $host = null;

/**
 * FrontendUriBuilder constructor.
 */
public function __construct()
{
}

/**
 * @param int $pageId the target pageId
 * @return $this FrontendUriBuilder
 */
public function setPageId($pageId = 1){
    $this->pageId = $pageId;

    return $this;
}

/**
 * @param string $extensionName 
 * @return $this FrontendUriBuilder
 */
public function setExtensionName($extensionName){
    $this->extensionName = $extensionName;

    return $this;
}

/**
 * @param string $pluginName
 * @return $this FrontendUriBuilder
 */
public function setPlugin($pluginName){
    $this->pluginName = $pluginName;

    return $this;
}

/**
 * @param string $actionName
 * @return $this FrontendUriBuilder
 */
public function setAction($actionName){
    $this->actionName = $actionName;

    return $this;
}

/**
 * @param string $controllerName
 * @return $this FrontendUriBuilder
 */
public function setController($controllerName){
    $this->controllerName = $controllerName;

    return $this;
}

/**
 * @param array $arguments like array('nameOfTheClass' => $instance) 
 * @return $this FrontendUriBuilder
 */
public function setArguments($arguments){
    $this->arguments = $arguments;

    return $this;
}

/**
 * @param string $host
 * @return $this FrontendUriBuilder
 */
public function setHost($host){
    $this->host = $host;

    return $this;
}

/**
 * Build the URL 
 * @return string the url
 * @throws \Exception
 */
public function build(){

    //set base
    $url = 'http://' . is_null($this->host)? $_SERVER['HTTP_HOST'] : $this->host;

    //set pageId
    $url = $url . '/index.php?id=' . $this->pageId;

    //set action
    if(!is_null($this->actionName)){
        $this->checkExtensionName();
        $this->checkPluginName();

        $url = $url . '&tx_' . $this->extensionName . '_' . $this->pluginName . '[action]=' . $this->actionName;
    }

    //set controller
    if(!is_null($this->controllerName)){
        $this->checkExtensionName();
        $this->checkPluginName();

        $url = $url . '&tx_' . $this->extensionName . '_' . $this->pluginName . '[controller]=' . ucfirst($this->controllerName);
    }

    //set arguments
    if(!is_null($this->arguments)) {
        $this->checkExtensionName();
        $this->checkPluginName();

        /**
         * @var $argument AbstractEntity
         */
        foreach ($this->arguments as $key => $argument) {
            $url = $url . '&tx_' . $this->extensionName . '_' . $this->pluginName . '[' . $key . ']=' . $argument->getUid();
        }
    }

    return $url;
}

private function checkExtensionName(){
    if(is_null($this->extensionName)){
        throw new \Exception("Extension name for FrontendUriBuilder not set!");
    }
}

private function checkPluginName(){
    if(is_null($this->pluginName)){
        throw new \Exception("Plugin name for FrontendUriBuilder not set!");
    }
}
}

你可以这样称呼它:

$uriBuilder = new FrontendUriBuilder();
$uriBuilder->setPageId($pageUid)->setExtensionName($extensionName)->setPlugin($pluginName)->setAction($action)->setController($controller)->setArguments($arguments)->build();

最小的调用是:

$uriBuilder->build();

这将使用 uid 1 创建一个 link 到您的主页。

如果您想向 url 添加动作、控制器或参数,您需要设置 "extensionName" 和 "pluginName"!

写在class

之前
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\HttpUtility;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;

创建link。

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

                        $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Extbase\Object\ObjectManager');

                        $configurationManager = $objectManager->get(ConfigurationManager::class);
                        /** @var ContentObjectRenderer $contentObjectRenderer */
                        $contentObjectRenderer = $objectManager->get(ContentObjectRenderer::class);

                        $configurationManager->setContentObject($contentObjectRenderer);
                        $uriBuilder = $objectManager->get(UriBuilder::class);
                        $uriBuilder->injectConfigurationManager($configurationManager);
                        $targetPage = $singleConfig['targetpid'];

    //Initialize object explicitly to set contentObject member of class (required for TYPO3 10.X)
    $uriBuilder->initializeObject();


                        $uriBuilder->setTargetPageUid($targetPage)->setArguments(array());

                        $url = $uriBuilder->buildFrontendUri();