我用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(),但错误仍然存在。我是完全错了还是错过了什么?
在类之前写这个
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'];
//Initialize object explicitly to set contentObject member of class (required for TYPO3 10.X)
$uriBuilder->initializeObject();
$uriBuilder->setTargetPageUid($targetPage)->setArguments(array());
$url = $uriBuilder->buildFrontendUri();
我找到了答案,但天知道这感觉太复杂了,不可能是对的。我所做的是在我的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并生成所需的链接。
我觉得自己很脏。
我创建了一个FrontendUriBuilderClass,使用它可以创建一个到带有参数等特定操作的前端链接。
有了这个helper类,就可以在后端创建一个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"answers"pluginName"!
什么类型的3版本?
尝试使用"build()"而不是"buildFrontendUri())",并让typep3识别环境。如果您在extbase控制器中,则不需要"创建"uribuilder
$uri = $this->uriBuilder->reset()->setTargetPageUid($pageUid)->setCreateAbsoluteUri(TRUE)->setArguments($arguments)->build();