Skip to content

Commit 5a27267

Browse files
committed
[FrameworkBundle] Configure RequestContext through router config
1 parent e608b27 commit 5a27267

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

DependencyInjection/Configuration.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,15 @@ private function addRouterSection(ArrayNodeDefinition $rootNode)
482482
->defaultTrue()
483483
->end()
484484
->booleanNode('utf8')->defaultFalse()->end()
485+
->arrayNode('context')
486+
->info('router request 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()
485494
->end()
486495
->end()
487496
->end()

DependencyInjection/FrameworkExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,11 @@ private function registerRouterConfiguration(array $config, ContainerBuilder $co
854854
$container->setParameter('request_listener.http_port', $config['http_port']);
855855
$container->setParameter('request_listener.https_port', $config['https_port']);
856856

857+
$requestContext = $container->findDefinition('router.request_context');
858+
$requestContext->replaceArgument(0, $config['context']['base_url']);
859+
$requestContext->replaceArgument(2, $config['context']['host']);
860+
$requestContext->replaceArgument(3, $config['context']['scheme']);
861+
857862
if ($this->annotationsConfigEnabled) {
858863
$container->register('routing.loader.annotation', AnnotatedRouteControllerLoader::class)
859864
->setPublic(false)

Resources/config/routing.xml

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

8181
<service id="router.request_context" class="Symfony\Component\Routing\RequestContext">
82-
<argument>%router.request_context.base_url%</argument>
82+
<argument></argument> <!-- base_url -->
8383
<argument>GET</argument>
84-
<argument>%router.request_context.host%</argument>
85-
<argument>%router.request_context.scheme%</argument>
84+
<argument></argument> <!-- host -->
85+
<argument></argument> <!-- scheme -->
8686
<argument>%request_listener.http_port%</argument>
8787
<argument>%request_listener.https_port%</argument>
8888
</service>

Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,11 @@ protected static function getBundleDefaultConfig()
413413
'https_port' => 443,
414414
'strict_requirements' => true,
415415
'utf8' => false,
416+
'context' => [
417+
'host' => '%router.request_context.host%',
418+
'scheme' => '%router.request_context.scheme%',
419+
'base_url' => '%router.request_context.base_url%',
420+
],
416421
],
417422
'session' => [
418423
'enabled' => false,

0 commit comments

Comments
 (0)