2015-11-17 51 views
1

我在TYPO3(用extbase)編寫了一個後端鉤子,用於發送電子郵件時設置了某些表的值。我現在需要做的就是生成一個前端URL。但我似乎無法控制UriBuilder。如何在後端鉤子上下文中構建有效的前端網址?我試過是這樣的:TYPO3與extbase:UriBuilder(或類似的)後端掛鉤

$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(),但錯誤仍然是一樣的。我是否完全錯誤地思考這個問題,或者我錯過了什麼?

回答

1

我找到了答案,但上帝知道這感覺太複雜了,以至於沒錯。我所做的就是建立在我的推廣和我的後端鉤一個前端Ajax調用我叫AJAX網頁中抓取的網址:

$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並生成所需的鏈接。

......我覺得很骯髒。

-1

什麼typo3版本?

嘗試使用「build()」而不是「buildFrontendUri()」,讓typo3識別環境。 如果你是一個extbase控制器裏面,你不應該需要「創造」的uribuilder

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

我已經創建了一個FrontendUriBuilderClass,與您可以創建一個前端鏈路以參數等具體行動。

有了這個幫手類,可以在後端創建一個Typo3前端鏈接。隨意使用它!

/** 
* 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

如果你想添加一個動作,控制器或參數到url,你需要設置「extensionName」和「pluginName」!

0

寫這

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; 

類之前創建的鏈接。

$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']; 
         $uriBuilder->setTargetPageUid($targetPage)->setArguments(array()); 

         $url = $uriBuilder->buildFrontendUri();