Issue Bug Closed

#105531Generated uri is always empty if UriBuilder is injected in constructor

David Hoeckele 11/01/24, 12:53 updated 01/28/26, 09:21

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.

Reviews 0

No change resolves or names this issue in its commit message, and no comment links one.

History 9

  1. Garvin Hicking commented and changed category status New Needs Feedback <span data-processor="relativetime" data-value="2024-11-01T14:06:34+00:00" > 11/01/24, 14:06 </span>

    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)?

  2. David Hoeckele commented <span data-processor="relativetime" data-value="2024-11-01T14:56:57+00:00" > 11/01/24, 14:56 </span>

    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'];
    $request = $this->renderingContext->getAttribute(ServerRequestInterface::class);
  3. Garvin Hicking commented <span data-processor="relativetime" data-value="2024-11-01T15:02:03+00:00" > 11/01/24, 15:02 </span>

    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?

  4. Garvin Hicking commented and changed status Needs Feedback Closed <span data-processor="relativetime" data-value="2024-11-02T09:44:35+00:00" > 11/02/24, 09:44 </span>

    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.

  5. Alexander Köberl commented <span data-processor="relativetime" data-value="2025-11-21T16:43:18+00:00" > 11/21/25, 16:43 </span>

    I also face this problem (empty url returned by uribuilder), but in my case the uribuilder is used in a middleware.
    I guess the request added to uribuilder with ->setRequest() is the problem.
    tried different aproaches to get the request, but failed.

    // Try #1: example from above: I do not have a renderingContext in a middleware
        $request = $this->renderingContext->getAttribute(ServerRequestInterface::class);
    
    // Try #2: use the ServerRequestInterface $request from process()
    public function process(ServerRequestInterface $request, \Psr\Http\Server\RequestHandlerInterface $handler): ResponseInterface
    {
        $this->request = $request;
        $uriBuilderRequest = new Request(
            $this->request->withAttribute('extbase', new ExtbaseRequestParameters())
        );
        $this->uriBuilder->setRequest($uriBuilderRequest);
    
    // Try #3: create a request
        $extbaseRequest = new ExtbaseRequest(
            $this->serverRequest->withAttribute('extbase', new ExtbaseRequestParameters())
        );
        $this->uriBuilder->setRequest($uriBuilderRequest);

    Failed with all attempts. $this->uriBuilder->build() returns an empty url.
    Whats the correct way to use uriBuilder in a middleware?

  6. Marc Willmann commented <span data-processor="relativetime" data-value="2026-01-07T15:19:06+00:00" > 01/07/26, 15:19 </span>

    Hi Alexander,

    in einer Middleware kannst Du die URI z.B. so generieren:

    try {
      /** @var Site $site */
      $site = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByPageId($pageId);
      $url = (string)$site->getRouter()->generateUri($pageId);
    } catch (SiteNotFoundException) {
      $url = '';
    }
  7. S P commented <span data-processor="relativetime" data-value="2026-01-28T09:15:31+00:00" > 01/28/26, 09:15 </span>

    @ghi Thanks for the doc update, but the issue is not actually fixed.

    The request for UriBuilder MUST be passed an instance of a ContentObjectRenderer as well, because it is doing return $currentContentObject?->createUrl($typolinkConfiguration) ?? '';
    This is the place where it actually goes wrong (i.e. an empty URL is returned).

    So the request MUST be enriched with it: ->withAttribute('currentContentObject', $contentObjectRenderer)

    However it is available – for example as $this->cObj when in DataProcessor context.

  8. Garvin Hicking commented <span data-processor="relativetime" data-value="2026-01-28T09:21:53+00:00" > 01/28/26, 09:21 </span>

    Hm, I’m not understanding where you would add/change what. Care to create a patch with your intent?