Created
February 26, 2013 11:08
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
... | |
?> |
I needed to make sure the routes configuration was loaded as well to make it work_
$flowSettings = $this->configurationManager->getConfiguration(\TYPO3\Flow\Configuration\ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, 'TYPO3.Flow');
$httpRequest = \TYPO3\Flow\Http\Request::create(new \TYPO3\Flow\Http\Uri('http://opeepl.dev'));
$httpRequest->injectSettings($flowSettings);
$request = new \TYPO3\Flow\Mvc\ActionRequest($httpRequest);
$uriBuilder = new \TYPO3\Flow\Mvc\Routing\UriBuilder();
$uriBuilder->setRequest($request);
$routesConfiguration = $this->configurationManager>getConfiguration(\TYPO3\Flow\Configuration\ConfigurationManager::CONFIGURATION_TYPE_ROUTES);
$router = \TYPO3\Flow\Reflection\ObjectAccess::getProperty($uriBuilder, 'router', TRUE);
$router->setRoutesConfiguration($routesConfiguration);
Nice snippet thanks!
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;
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
Exactly what I was looking for, thank you!