Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Default value for route host is not used if context::$host is empty (fx in command) #25464

Closed
@benjaminrau

Description

@benjaminrau
Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 3.1.6

Relates: #21971

About

@xabbuh @Tobion I am not sure if i understand the issue right but i stumpled over this post when i searched for existing tickets for my issue - which sounds quite similar.

If context::$host is empty, fx if you generate url from a command, then the $hostTokens are not replaced because this foreach loop is nested inside a condition which checks if host is set.

This means that although you have set default host for this route its not going to be replaced because host is empty.

Code

routes.yml

frontend_options:
    resource: angular_routing_resources.yml
    host: "{domain}"
    schemes:  ['http']
    defaults:
      domain: 'my.fallback.host'
    requirements:
      domain: .+

\Symfony\Component\Routing\Generator\UrlGenerator::doGenerate (Symfony/Component/Routing/Generator/UrlGenerator.php:201)

if ($host = $this->context->getHost()) {
// Some not relevant code comes here...
// But then, still inside this if clause
    if ($routeHost !== $host) {
            $host = $routeHost;
            if (self::ABSOLUTE_URL !== $referenceType) {
                 $referenceType = self::NETWORK_PATH;
            }
     }
}

Actual result

$url = $router->generate($type, $params, UrlGeneratorInterface::ABSOLUTE_URL); // $url = "/route/to/target"

Workaround for expected result

If you set a host (no matter what) the relevant code will be reached and thus the default value for host used.

$router->getContext()->setHost("whatever");
$url = $router->generate($type, $params, UrlGeneratorInterface::ABSOLUTE_URL); // $url = "http://my.fallback.host/route/to/target"

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions