Skip to content

Commit 5fa87cd

Browse files
Add condition option on legacy routing (#1048)
| Q | A | --------------- | ----- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Related tickets | | License | MIT To create a bc-layer for the legacy routes in Sylius E-commerce project, I need to add some route conditions to existing routes. On simple route, I'm able to create this condition: ```yaml sylius_admin_product_create_simple: path: /products/new/simple methods: [GET, POST] condition: "context.isSyliusRoutingBcLayerEnabled() || context.isSyliusRoutingBcLayerEnabled('admin_product')" # ... ``` But I also need to add this condition on multiple routing definition like this following one and that's the reason of this PR. ```yaml sylius_admin_product: resource: | alias: sylius.product condition: "context.isSyliusRoutingBcLayerEnabled() || context.isSyliusRoutingBcLayerEnabled('admin_product')" # ... type: sylius.resource ``` To bring more context, here is the RequestContext we will implement on Sylius core to create the bc-layer ```php namespace Sylius\Bundle\CoreBundle\Routing; use Symfony\Component\Routing\RequestContext as BaseRequestContext; final class RequestContext extends BaseRequestContext { public function __construct( BaseRequestContext $decorated, private array $bcLayerConfig, ) { // ... } public function isSyliusRoutingBcLayerEnabled(?string $key = null): bool { if (null === $key) { return $this->bcLayerConfig['enabled'] ?? false; } return $this->bcLayerConfig['routes'][$key]['enabled'] ?? false; } } ```
2 parents 6e74dde + 9e9e8fa commit 5fa87cd

File tree

3 files changed

+188
-79
lines changed

3 files changed

+188
-79
lines changed

src/Bundle/Routing/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function getConfigTreeBuilder(): TreeBuilder
4343
->scalarNode('templates')->cannotBeEmpty()->end()
4444
->scalarNode('grid')->cannotBeEmpty()->end()
4545
->booleanNode('permission')->defaultValue(false)->end()
46+
->scalarNode('condition')->cannotBeEmpty()->end()
4647
->arrayNode('except')
4748
->scalarPrototype()->end()
4849
->end()

src/Bundle/Routing/ResourceLoader.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ private function createRoute(
179179
];
180180
}
181181

182-
return $this->routeFactory->createRoute($path, $defaults, [], [], '', [], $methods);
182+
$condition = $configuration['condition'] ?? '';
183+
184+
return $this->routeFactory->createRoute($path, $defaults, [], [], '', [], $methods, $condition);
183185
}
184186

185187
private function getRouteName(MetadataInterface $metadata, array $configuration, string $actionName): string

0 commit comments

Comments
 (0)