8000 Custom base URL support in UrlGenerator for absolute URLs · Issue #60187 · symfony/symfony · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Custom base URL support in UrlGenerator for absolute URLs #60187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
speller opened this issue Apr 9, 2025 · 2 comments
Open

Custom base URL support in UrlGenerator for absolute URLs #60187

speller opened this issue Apr 9, 2025 · 2 comments

Comments

@speller
Copy link
speller commented Apr 9, 2025

Description

The proposal is to allow passing a custom base URL for absolute URLs, not just the current/preconfigured context one.

Use case:
My website is working on multiple domains. It is not possible to preconfigure a single one. I need to be able to read the base URL from some config (in yaml or in database) and pass it to the URL builder to make an absolute URL for a specific domain. For example - email generation for different users having sites on different domains where links to their websites must use a specific base URL.

Example

Here is how I solved it in my project with a patch:

--- a/Generator/UrlGenerator.php
+++ b/Generator/UrlGenerator.php
@@ -199,8 +199,10 @@
         }

         $schemeAuthority = '';
-        $host = $this->context->getHost();
-        $scheme = $this->context->getScheme();
+        $host = $parameters['_host'] ?? $this->context->getHost();
+        $port = $parameters['_port'] ?? '';
+        $scheme = $parameters['_scheme'] ?? $this->context->getScheme();
+        unset($parameters['_scheme'], $parameters['_host'], $parameters['_port']);

         if ($requiredSchemes) {
             if (!\in_array($scheme, $requiredSchemes, true)) {
@@ -240,11 +242,10 @@

         if (self::ABSOLUTE_URL === $referenceType || self::NETWORK_PATH === $referenceType) {
             if ('' !== $host || ('' !== $scheme && 'http' !== $scheme && 'https' !== $scheme)) {
-                $port = '';
-                if ('http' === $scheme && 80 !== $this->context->getHttpPort()) {
-                    $port = ':'.$this->context->getHttpPort();
-                } elseif ('https' === $scheme && 443 !== $this->context->getHttpsPort()) {
-                    $port = ':'.$this->context->getHttpsPort();
+                if ('http' === $scheme && 80 !== ($port ?: $this->context->getHttpPort())) {
+                    $port = ':'.($port ?: $this->context->getHttpPort());
+                } elseif ('https' === $scheme && 443 !== ($port ?: $this->context->getHttpsPort())) {
+                    $port = ':'.($port ?: $this->context->getHttpsPort());
                 }

                 $schemeAuthority = self::NETWORK_PATH === $referenceType || '' === $scheme ? '//' : "$scheme://";

Example usage:

        return $this->urlGenerator->generate(
            'public_order_tracking',
            [
                ...
                '_scheme' => $base['scheme'] ?? null,
                '_host' => $base['host'] ?? null,
                '_port' => $base['port'] ?? null,
            ],
            UrlGeneratorInterface::ABSOLUTE_URL,
        );
@pyatnitsev
Copy link
pyatnitsev commented Apr 14, 2025

Maybe better solution would be an DTO object, that could configure that props?
something like that:


urlGenerator->generate('route_name', [], UrlGeneratorInterface::ABSOLUTE_URL, new UrlConfigurator()->setHost('example.com'));

@kevinpapst
Copy link

What about using the existing setContext(RequestContext $context) / getContext()->setHost('example.com') methods?

I am running a multi-tenant application on subdomains and only had that requirement for email generation in commands.
You could call the methods e.g. from an EventSubscriber listening on KernelEvents or ConsoleEvents or wherever you need it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants
0