Skip to content

Commit 3844252

Browse files
committed
Se pueden importar rutas en la configuración YAML.
1 parent 9119743 commit 3844252

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

config/routes.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Base configuration for routes.yaml
22

3-
- route: /
3+
homepage:
4+
path: /
45
handler: '%kernel.project_dir%/README.md'
5-
name: homepage
66

7-
- route: error
7+
errorpage:
8+
path: error
89
handler: '%kernel.project_dir%/templates/error.html.twig'
9-
name: errorpage

src/Config/Loader/YamlRoutesLoader.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ public function __construct(
3939
*/
4040
public function load($resource, ?string $type = null): mixed
4141
{
42+
// Load main routes.
4243
$routes = Yaml::parseFile($resource);
4344

45+
// Check if the routes are an array.
4446
if (!is_array($routes)) {
4547
throw new InvalidArgumentException(sprintf(
4648
'The YAML file "%s" has an invalid type, got %s.',
@@ -49,8 +51,26 @@ public function load($resource, ?string $type = null): mixed
4951
));
5052
}
5153

54+
// Load imported routes.
55+
if (isset($routes['imports'])) {
56+
$importedRoutes = [];
57+
foreach ($routes['imports'] as $import) {
58+
$importedRoutes = array_merge(
59+
$importedRoutes,
60+
$this->load(
61+
dirname($resource) . '/' . $import['resource'],
62+
$type
63+
)
64+
);
65+
}
66+
unset($routes['imports']);
67+
$routes = array_merge($importedRoutes, $routes);
68+
}
69+
70+
// Set routes parameter.
5271
$this->container->setParameter('routes', $routes);
5372

73+
// Return routes.
5474
return $routes;
5575
}
5676

0 commit comments

Comments
 (0)