Skip to content

Commit 44d54f5

Browse files
committed
feature #36651 [FrameworkBundle] Allow configuring the default base URI with a DSN (nicolas-grekas)
This PR was merged into the 5.1-dev branch. Discussion ---------- [FrameworkBundle] Allow configuring the default base URI with a DSN | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fixes #35121, replaces #35580, partially reverts #35281 | License | MIT | Doc PR | - Instead of defining 3-4 parameters, this PR enables using a single DSN to configure the default URL context (for commands mainly): ``` framework: router: base_uri: 'https://my.host:8443/base-path/' ``` When using parameters directly, one can now set the same absolute URI in the `router.request_context.base_url` parameter, this will provide the same benefit. Commits ------- 250fa7e979 [FrameworkBundle] Allow configuring the default base URI with a DSN
2 parents 8245e62 + 5e0807c commit 44d54f5

File tree

5 files changed

+16
-25
lines changed

5 files changed

+16
-25
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CHANGELOG
66

77
* Added link to source for controllers registered as named services
88
* Added link to source on controller on `router:match`/`debug:router` (when `framework.ide` is configured)
9-
* Added the `framework.router.context` configuration node to configure the `RequestContext`
9+
* Added the `framework.router.default_uri` configuration option to configure the default `RequestContext`
1010
* Made `MicroKernelTrait::configureContainer()` compatible with `ContainerConfigurator`
1111
* Added a new `mailer.message_bus` option to configure or disable the message bus to use to send mails.
1212
* Added flex-compatible default implementations for `MicroKernelTrait::registerBundles()` and `getProjectDir()`

DependencyInjection/Configuration.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,10 @@ private function addRouterSection(ArrayNodeDefinition $rootNode)
470470
->children()
471471
->scalarNode('resource')->isRequired()->end()
472472
->scalarNode('type')->end()
473+
->scalarNode('default_uri')
474+
->info('The default URI used to generate URLs in a non-HTTP context')
475+
->defaultNull()
476+
->end()
473477
->scalarNode('http_port')->defaultValue(80)->end()
474478
->scalarNode('https_port')->defaultValue(443)->end()
475479
->scalarNode('strict_requirements')
@@ -482,15 +486,6 @@ private function addRouterSection(ArrayNodeDefinition $rootNode)
482486
->defaultTrue()
483487
->end()
484488
->booleanNode('utf8')->defaultNull()->end()
485-
->arrayNode('context')
486-
->info('The request context used to generate URLs in a non-HTTP context')
487-
->addDefaultsIfNotSet()
488-
->children()
489-
->scalarNode('host')->defaultValue('%router.request_context.host%')->end()
490-
->scalarNode('scheme')->defaultValue('%router.request_context.scheme%')->end()
491-
->scalarNode('base_url')->defaultValue('%router.request_context.base_url%')->end()
492-
->end()
493-
->end()
494489
->end()
495490
->end()
496491
->end()

DependencyInjection/FrameworkExtension.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -899,10 +899,10 @@ private function registerRouterConfiguration(array $config, ContainerBuilder $co
899899
$container->setParameter('request_listener.http_port', $config['http_port']);
900900
$container->setParameter('request_listener.https_port', $config['https_port']);
901901

902-
$requestContext = $container->getDefinition('router.request_context');
903-
$requestContext->replaceArgument(0, $config['context']['base_url']);
904-
$requestContext->replaceArgument(2, $config['context']['host']);
905-
$requestContext->replaceArgument(3, $config['context']['scheme']);
902+
if (null !== $config['default_uri']) {
903+
$container->getDefinition('router.request_context')
904+
->replaceArgument(0, $config['default_uri']);
905+
}
906906

907907
if ($this->annotationsConfigEnabled) {
908908
$container->register('routing.loader.annotation', AnnotatedRouteControllerLoader::class)

Resources/config/routing.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@
8080
<service id="Symfony\Component\Routing\RequestContextAwareInterface" alias="router" />
8181

8282
<service id="router.request_context" class="Symfony\Component\Routing\RequestContext">
83-
<argument></argument> <!-- base_url -->
84-
<argument>GET</argument>
85-
<argument></argument> <!-- host -->
86-
<argument></argument> <!-- scheme -->
83+
<factory class="Symfony\Component\Routing\RequestContext" method="fromUri" />
84+
<argument>%router.request_context.base_url%</argument>
85+
<argument>%router.request_context.host%</argument>
86+
<argument>%router.request_context.scheme%</argument>
8787
<argument>%request_listener.http_port%</argument>
8888
<argument>%request_listener.https_port%</argument>
8989
<call method="setParameter">
@@ -117,8 +117,8 @@
117117

118118
<service id="Symfony\Bundle\FrameworkBundle\Controller\RedirectController" public="true">
119119
<argument type="service" id="router" />
120-
<argument>%request_listener.http_port%</argument>
121-
<argument>%request_listener.https_port%</argument>
120+
<argument type="service"><service class="int"><factory service="router.request_context" method="getHttpPort" /></service></argument>
121+
<argument type="service"><service class="int"><factory service="router.request_context" method="getHttpsPort" /></service></argument>
122122
</service>
123123

124124
<service id="Symfony\Bundle\FrameworkBundle\Controller\TemplateController" public="true">

Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -410,15 +410,11 @@ protected static function getBundleDefaultConfig()
410410
],
411411
'router' => [
412412
'enabled' => false,
413+
'default_uri' => null,
413414
'http_port' => 80,
414415
'https_port' => 443,
415416
'strict_requirements' => true,
416417
'utf8' => null,
417-
'context' => [
418-
'host' => '%router.request_context.host%',
419-
'scheme' => '%router.request_context.scheme%',
420-
'base_url' => '%router.request_context.base_url%',
421-
],
422418
],
423419
'session' => [
424420
'enabled' => false,

0 commit comments

Comments
 (0)