diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index 98966a5a18..686d3c2ecb 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -6,7 +6,7 @@ CHANGELOG * Added link to source for controllers registered as named services * Added link to source on controller on `router:match`/`debug:router` (when `framework.ide` is configured) - * Added the `framework.router.context` configuration node to configure the `RequestContext` + * Added the `framework.router.default_uri` configuration option to configure the default `RequestContext` * Made `MicroKernelTrait::configureContainer()` compatible with `ContainerConfigurator` * Added a new `mailer.message_bus` option to configure or disable the message bus to use to send mails. * Added flex-compatible default implementations for `MicroKernelTrait::registerBundles()` and `getProjectDir()` diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index 7caec9b49f..eab898b882 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -470,6 +470,10 @@ class Configuration implements ConfigurationInterface ->children() ->scalarNode('resource')->isRequired()->end() ->scalarNode('type')->end() + ->scalarNode('default_uri') + ->info('The default URI used to generate URLs in a non-HTTP context') + ->defaultNull() + ->end() ->scalarNode('http_port')->defaultValue(80)->end() ->scalarNode('https_port')->defaultValue(443)->end() ->scalarNode('strict_requirements') @@ -482,15 +486,6 @@ class Configuration implements ConfigurationInterface ->defaultTrue() ->end() ->booleanNode('utf8')->defaultNull()->end() - ->arrayNode('context') - ->info('The request context used to generate URLs in a non-HTTP context') - ->addDefaultsIfNotSet() - ->children() - ->scalarNode('host')->defaultValue('%router.request_context.host%')->end() - ->scalarNode('scheme')->defaultValue('%router.request_context.scheme%')->end() - ->scalarNode('base_url')->defaultValue('%router.request_context.base_url%')->end() - ->end() - ->end() ->end() ->end() ->end() diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 5ad7341279..666797f203 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -899,10 +899,10 @@ class FrameworkExtension extends Extension $container->setParameter('request_listener.http_port', $config['http_port']); $container->setParameter('request_listener.https_port', $config['https_port']); - $requestContext = $container->getDefinition('router.request_context'); - $requestContext->replaceArgument(0, $config['context']['base_url']); - $requestContext->replaceArgument(2, $config['context']['host']); - $requestContext->replaceArgument(3, $config['context']['scheme']); + if (null !== $config['default_uri']) { + $container->getDefinition('router.request_context') + ->replaceArgument(0, $config['default_uri']); + } if ($this->annotationsConfigEnabled) { $container->register('routing.loader.annotation', AnnotatedRouteControllerLoader::class) diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml index cf662e2748..669b27d72c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml @@ -80,10 +80,10 @@ - - GET - - + + %router.request_context.base_url% + %router.request_context.host% + %router.request_context.scheme% %request_listener.http_port% %request_listener.https_port% @@ -117,8 +117,8 @@ - %request_listener.http_port% - %request_listener.https_port% + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php index fad9b09a04..3ba4c3ecfe 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php @@ -410,15 +410,11 @@ class ConfigurationTest extends TestCase ], 'router' => [ 'enabled' => false, + 'default_uri' => null, 'http_port' => 80, 'https_port' => 443, 'strict_requirements' => true, 'utf8' => null, - 'context' => [ - 'host' => '%router.request_context.host%', - 'scheme' => '%router.request_context.scheme%', - 'base_url' => '%router.request_context.base_url%', - ], ], 'session' => [ 'enabled' => false, diff --git a/src/Symfony/Bundle/SecurityBundle/Resources/config/security_listeners.xml b/src/Symfony/Bundle/SecurityBundle/Resources/config/security_listeners.xml index 8b14cfd9e0..10b503b6bf 100644 --- a/src/Symfony/Bundle/SecurityBundle/Resources/config/security_listeners.xml +++ b/src/Symfony/Bundle/SecurityBundle/Resources/config/security_listeners.xml @@ -20,8 +20,8 @@ - %request_listener.http_port% - %request_listener.https_port% + + diff --git a/src/Symfony/Component/Routing/CHANGELOG.md b/src/Symfony/Component/Routing/CHANGELOG.md index 267372eb82..d14549b522 100644 --- a/src/Symfony/Component/Routing/CHANGELOG.md +++ b/src/Symfony/Component/Routing/CHANGELOG.md @@ -12,6 +12,7 @@ CHANGELOG * added `ExpressionLanguageProvider` to expose extra functions to route conditions * added support for a `stateless` keyword for configuring route stateless in PHP, YAML and XML configurations. * added the "hosts" option to be able to configure the host per locale. + * added `RequestContext::fromUri()` to ease building the default context 5.0.0 ----- diff --git a/src/Symfony/Component/Routing/RequestContext.php b/src/Symfony/Component/Routing/RequestContext.php index cb655f4485..17fc021c3c 100644 --- a/src/Symfony/Component/Routing/RequestContext.php +++ b/src/Symfony/Component/Routing/RequestContext.php @@ -45,6 +45,23 @@ class RequestContext $this->setQueryString($queryString); } + public static function fromUri(string $uri, string $host = 'localhost', string $scheme = 'http', int $httpPort = 80, int $httpsPort = 443): self + { + $uri = parse_url($uri); + $scheme = $uri['scheme'] ?? $scheme; + $host = $uri['host'] ?? $host; + + if (isset($uri['port'])) { + if ('http' === $scheme) { + $httpPort = $uri['port']; + } elseif ('https' === $scheme) { + $httpsPort = $uri['port']; + } + } + + return new self($uri['path'] ?? '', 'GET', $host, $scheme, $httpPort, $httpsPort); + } + /** * Updates the RequestContext information based on a HttpFoundation Request. *