我在TYPO3(带有extbase)中编写了一个后端挂钩,用于在设置某些表的值时发送电子邮件。我现在需要做的就是生成一个前端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(),但错误仍然相同。我是以完全错误的方式解决这个问题还是我错过了什么?
答案 0 :(得分:1)
我找到了一个答案,但上帝知道这种感觉太复杂而不是正确的。我所做的是在我的Extension中创建一个前端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并生成所需的链接外什么都不做。
......我觉得很脏。
答案 1 :(得分: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”!
答案 2 :(得分: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();
答案 3 :(得分:-1)
什么是typo3版本?
尝试使用" build()"而不是" buildFrontendUri()"让typo3认识环境。 如果你在一个extbase控制器内,你不需要"创建" uribuilder
$uri = $this->uriBuilder->reset()->setTargetPageUid($pageUid)->setCreateAbsoluteUri(TRUE)->setArguments($arguments)->build();