Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 14d18fa

Browse files
committed
Merge branch 'hotfix/596'
Close #596
2 parents ddb7979 + 29a9ef1 commit 14d18fa

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ All notable changes to this project will be documented in this file, in reverse
1010

1111
### Changed
1212

13-
- Nothing.
13+
- [#596](https://github.com/zendframework/zend-expressive/pull/596) updates the
14+
`ApplicationConfigInjectionDelegator::injectRoutesFromConfig()` method to use
15+
the key name associated with a route specification if no `name` member is
16+
provided when creating a `Route` instance. This can help enforce name
17+
uniqueness when defining routes via configuration.
1418

1519
### Deprecated
1620

docs/book/v3/cookbook/autowiring-routes-and-pipelines.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ return [
4040
'the' => 'underlying router',
4141
],
4242
],
43+
'another.route.name' => [
44+
'path' => '/another/path/to/match',
45+
'middleware' => 'Middleware service or pipeline',
46+
'allowed_methods' => ['GET', 'POST'],
47+
'options' => [
48+
'more' => 'router',
49+
'options' => 'here',
50+
],
51+
],
4352
],
4453
];
4554
```
@@ -97,7 +106,9 @@ following keys:
97106
the HTTP methods allowed for the route. If this is omitted, the assumption is
98107
any method is allowed.
99108
- `name` (optional, string): the name of the route, if any; this can be used
100-
later to generate a URI based on the route, and must be unique.
109+
later to generate a URI based on the route, and must be unique. The name may
110+
also be set using a string key in the routes configuration array. If both are
111+
set the name assigned in the spec will be used.
101112
- `options` (optional, array): any options to provide to the generated route.
102113
These might be default values or constraints, depending on the router
103114
implementation.

src/Container/ApplicationConfigInjectionDelegator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public static function injectRoutesFromConfig(Application $application, array $c
176176
return;
177177
}
178178

179-
foreach ($config['routes'] as $spec) {
179+
foreach ($config['routes'] as $key => $spec) {
180180
if (! isset($spec['path']) || ! isset($spec['middleware'])) {
181181
continue;
182182
}
@@ -192,7 +192,7 @@ public static function injectRoutesFromConfig(Application $application, array $c
192192
}
193193
}
194194

195-
$name = $spec['name'] ?? null;
195+
$name = $spec['name'] ?? (is_string($key) ? $key : null);
196196
$route = $application->route(
197197
$spec['path'],
198198
$spec['middleware'],

test/Container/ApplicationConfigInjectionDelegatorTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,49 @@ public function testInjectRoutesFromConfigWillSkipSpecsThatOmitMiddleware()
391391
$this->assertEquals([], $app->getRoutes());
392392
}
393393

394+
public function testInjectRoutesFromConfigSetRouteNameViaArrayKey()
395+
{
396+
$config = [
397+
'routes' => [
398+
'home' => [
399+
'path' => '/',
400+
'middleware' => new TestAsset\InteropMiddleware(),
401+
],
402+
],
403+
];
404+
$this->container->has('config')->willReturn(false);
405+
$app = $this->createApplication();
406+
407+
ApplicationConfigInjectionDelegator::injectRoutesFromConfig($app, $config);
408+
409+
$routes = $app->getRoutes();
410+
411+
$route = array_shift($routes);
412+
$this->assertEquals('home', $route->getName());
413+
}
414+
415+
public function testInjectRoutesFromConfigRouteSpecNameOverrideArrayKeyName()
416+
{
417+
$config = [
418+
'routes' => [
419+
'home' => [
420+
'name' => 'homepage',
421+
'path' => '/',
422+
'middleware' => new TestAsset\InteropMiddleware(),
423+
],
424+
],
425+
];
426+
$this->container->has('config')->willReturn(false);
427+
$app = $this->createApplication();
428+
429+
ApplicationConfigInjectionDelegator::injectRoutesFromConfig($app, $config);
430+
431+
$routes = $app->getRoutes();
432+
433+
$route = array_shift($routes);
434+
$this->assertEquals('homepage', $route->getName());
435+
}
436+
394437
public function testInjectPipelineFromConfigRaisesExceptionForSpecsOmittingMiddlewareKey()
395438
{
396439
$config = [

0 commit comments

Comments
 (0)