Bug #72706
closed
Manually instantiated UriBuilder is incomplete
Added by Aimeos no-lastname-given almost 9 years ago.
Updated over 4 years ago.
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.
- 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?
- 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.
Thanks a lot!
How do we get the $site variable? Can you post the code that is necessary for setting it up that?
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]);
- Related to Bug #88506: Scheduler generated URLs contain absolute unix paths added
- Status changed from Needs Feedback to Resolved
- Status changed from Resolved to Closed
Also available in: Atom
PDF