Manually instantiated UriBuilder is incomplete
When trying to instantiate the UriBuilder object in a scheduler task using the object manager, the resulting object is incomplete, esp. the ContentObjectRenderer instance is missing. This results in a fatal error when trying to generate a link.
Example code in scheduler task:
$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( \TYPO3\CMS\Extbase\Object\ObjectManager::class ); $uriBuilder = $objectManager->get( \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class ); $uriBuilder->setTargetPageUid( $pageid )->setArguments( array() ); $url = $uriBuilder->buildFrontendUri();
It worked from 7.0 to at least 7.3 but seemed to vanish in 7.4 again.
#1 Updated by Christian Eßl 9 months ago
- TYPO3 Version changed from 7 to 10
This is still an issue in master and the problem is pretty well known and clear:
You need a valid frontend context for the UriBuilder to be able to correctly call UriBuilder::buildFrontendUri(). This means you can't build frontend uris in backend or cli context.There are several workarounds available like:
- manually setting up a TypoScriptFrontendController for "simulating" the frontend (which is really ugly)
- using third party extensions like "typoscript_rendering"
But since TYPO3 9.5, the new Routing API is available and already used in other parts in the core (Have a look at BackendUtility::getPreviewUrl()). I wonder if the routing API could be used in the UriBuilder as well for easily building frontend uris independent of the callers context? Or are there some edge cases or other issues that could arise from that?