Skip to content

Instantly share code, notes, and snippets.

@agitso
Created February 26, 2013 11:08
Show Gist options
  • Save agitso/5037717 to your computer and use it in GitHub Desktop.
Save agitso/5037717 to your computer and use it in GitHub Desktop.
Use TYPO3 Flow UriBuilder in a non-controller context (fx. in a service that is used by command controller)
<?php
...
/**
* @var \TYPO3\Flow\Mvc\Routing\UriBuilder
*/
protected $uriBuilder;
/**
* @param \TYPO3\Flow\Configuration\ConfigurationManager $configurationManager
*/
public function injectUriBuilder(\TYPO3\Flow\Configuration\ConfigurationManager $configurationManager) {
$flowSettings = $configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, 'TYPO3.Flow');
$httpRequest = \TYPO3\Flow\Http\Request::create(new \TYPO3\Flow\Http\Uri('http://localhost'));
$httpRequest->injectSettings($flowSettings);
$request = new \TYPO3\Flow\Mvc\ActionRequest($httpRequest);
$uriBuilder = new \TYPO3\Flow\Mvc\Routing\UriBuilder();
$uriBuilder->setRequest($request);
$uriBuilder->setCreateAbsoluteUri(TRUE);
$this->uriBuilder = $uriBuilder;
}
...
?>
@svparijs
Copy link

Nice snippet thanks!

@Duske
Copy link

Duske commented Jun 18, 2015

The httpRequest can be constructed with createFromEnvironment() now, so this worked for me:

$httpRequest = \TYPO3\Flow\Http\Request::createFromEnvironment();
$request = new \TYPO3\Flow\Mvc\ActionRequest($httpRequest);
$uriBuilder = new \TYPO3\Flow\Mvc\Routing\UriBuilder();
$uriBuilder->setRequest($request);
$this->uriBuilder = $uriBuilder;

@micron
Copy link

micron commented May 10, 2017

Here is an example if you're using Extbase:

$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Extbase\Object\ObjectManager');
$request = $objectManager->get(\TYPO3\CMS\Extbase\Mvc\Web\Request::class);
$request->setRequestURI(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'));
$request->setBaseURI(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL'));
$this->uriBuilder = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class);
$this->uriBuilder->setRequest($request);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment