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

Commit

Permalink
Merge branch 'hotfix/596'
Browse files Browse the repository at this point in the history
Close #596
  • Loading branch information
weierophinney committed Mar 19, 2018
2 parents ddb7979 + 29a9ef1 commit 14d18fa
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ All notable changes to this project will be documented in this file, in reverse

### Changed

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

### Deprecated

Expand Down
13 changes: 12 additions & 1 deletion docs/book/v3/cookbook/autowiring-routes-and-pipelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ return [
'the' => 'underlying router',
],
],
'another.route.name' => [
'path' => '/another/path/to/match',
'middleware' => 'Middleware service or pipeline',
'allowed_methods' => ['GET', 'POST'],
'options' => [
'more' => 'router',
'options' => 'here',
],
],
],
];
```
Expand Down Expand Up @@ -97,7 +106,9 @@ following keys:
the HTTP methods allowed for the route. If this is omitted, the assumption is
any method is allowed.
- `name` (optional, string): the name of the route, if any; this can be used
later to generate a URI based on the route, and must be unique.
later to generate a URI based on the route, and must be unique. The name may
also be set using a string key in the routes configuration array. If both are
set the name assigned in the spec will be used.
- `options` (optional, array): any options to provide to the generated route.
These might be default values or constraints, depending on the router
implementation.
Expand Down
4 changes: 2 additions & 2 deletions src/Container/ApplicationConfigInjectionDelegator.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public static function injectRoutesFromConfig(Application $application, array $c
return;
}

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

$name = $spec['name'] ?? null;
$name = $spec['name'] ?? (is_string($key) ? $key : null);
$route = $application->route(
$spec['path'],
$spec['middleware'],
Expand Down
43 changes: 43 additions & 0 deletions test/Container/ApplicationConfigInjectionDelegatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,49 @@ public function testInjectRoutesFromConfigWillSkipSpecsThatOmitMiddleware()
$this->assertEquals([], $app->getRoutes());
}

public function testInjectRoutesFromConfigSetRouteNameViaArrayKey()
{
$config = [
'routes' => [
'home' => [
'path' => '/',
'middleware' => new TestAsset\InteropMiddleware(),
],
],
];
$this->container->has('config')->willReturn(false);
$app = $this->createApplication();

ApplicationConfigInjectionDelegator::injectRoutesFromConfig($app, $config);

$routes = $app->getRoutes();

$route = array_shift($routes);
$this->assertEquals('home', $route->getName());
}

public function testInjectRoutesFromConfigRouteSpecNameOverrideArrayKeyName()
{
$config = [
'routes' => [
'home' => [
'name' => 'homepage',
'path' => '/',
'middleware' => new TestAsset\InteropMiddleware(),
],
],
];
$this->container->has('config')->willReturn(false);
$app = $this->createApplication();

ApplicationConfigInjectionDelegator::injectRoutesFromConfig($app, $config);

$routes = $app->getRoutes();

$route = array_shift($routes);
$this->assertEquals('homepage', $route->getName());
}

public function testInjectPipelineFromConfigRaisesExceptionForSpecsOmittingMiddlewareKey()
{
$config = [
Expand Down

0 comments on commit 14d18fa

Please sign in to comment.