Bug #72706
closedManually instantiated UriBuilder is incomplete
0%
Description
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.
Updated by Christian Eßl over 5 years 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?
Updated by Benni Mack almost 5 years ago
- Status changed from New to Needs Feedback
Since TYPO3 v9 / site handling you don't need TSFE or cObject anymore to generate a link to a page.
$site->getRouter->buildUri($pageId).
This works in CLI and Backend environment as well.
Updated by Aimeos no-lastname-given almost 5 years ago
Thanks a lot!
How do we get the $site variable? Can you post the code that is necessary for setting it up that?
Updated by Benni Mack almost 5 years ago
In TYPO3 v9, try out the SiteFinder class, there is a class "getSiteByPageId($pid)".
$pageId = 13; $site = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByPageId($pageId); $site->getRouter()->generateUri($pageId, ['my-argument' => 13]);
Updated by Benni Mack almost 5 years ago
- Related to Bug #88506: Scheduler generated URLs contain absolute unix paths added
Updated by Georg Ringer almost 5 years ago
- Status changed from Needs Feedback to Resolved
setting this as solved