Bug #105531
closedGenerated uri is always empty if UriBuilder is injected in constructor
0%
Description
I tried to generate an uri in a ViewHelper with the UriBuilder, the way described in documentation: https://docs.typo3.org/m/typo3/reference-coreapi/13.4/en-us/ExtensionArchitecture/Extbase/Reference/UriBuilder.html
The generated uri is always empty string. Same result with GeneralUtility::makeInstance. I tried a second example like this:
$uri = $this->uriBuilder
->reset()
->setRequest($this->getExtbaseRequest())
->setTargetPageUid(1)
->build();
and the generated link is empty string, too.
Updated by Garvin Hicking 21 days ago
- Category set to Extbase
- Status changed from New to Needs Feedback
The docs actually don't aim at ViewHelpers (yet) where constructor based DI isn't available for now.
You can use the injectXxx Method, but actually also makeInstance should work. The core viewhelper f:link.action uses it like this, see https://github.com/TYPO3/typo3/blob/main/typo3/sysext/fluid/Classes/ViewHelpers/Uri/ActionViewHelper.php#L214C14-L214C17
Can you please show a full (minimal) ViewHelper example where this doesn't work? And in which context you use it (viewfactory, standaloneview, frontend/backend)?
Updated by David Hoeckele 21 days ago
OK, i didn't know that - sorry. Documentation only said "can be injected via constructor in a class", so i thought ...
It's used like this in frontend context (simplified example, upgraded from v12):
public function render(): string
{
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
$request = $GLOBALS['TYPO3_REQUEST'];
$uriBuilderRequest = new Request(
$request->withAttribute('extbase', new ExtbaseRequestParameters())
);
$uri = $uriBuilder
->reset()
->setRequest($uriBuilderRequest)
->setTargetPageType(1234567)
->setCreateAbsoluteUri(true)
->uriFor(
'download',
[ 'guid' => $guid ],
'Download',
'ExtensionName',
'PluginName'
);
return $uri;
}
The hint to the f:link.action ViewHelper was helpful - replacing
$request = $GLOBALS['TYPO3_REQUEST'];
with $request = $this->renderingContext->getAttribute(ServerRequestInterface::class);
does the trick.Updated by Garvin Hicking 21 days ago
Thanks for getting back so quick. I'll see to update the docs to specifically mention this, and will close the issue when I have done so, yes?
Updated by Garvin Hicking 20 days ago
- Status changed from Needs Feedback to Closed
Created a PR here: https://github.com/TYPO3-Documentation/TYPO3CMS-Reference-CoreApi/pull/4984
Closing the issue here now because of that. Feel free to check if the docs patch is good for you or if you have suggestions.
I'm trying to avoid accessing TYPO3_REQUEST in the v12-compatible code fork because it'll go away in the future anways; so better to start seeing the other ways to get the request context.