Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/RouteCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(RouteParser $routeParser, DataGenerator $dataGenerat
public function addRoute($httpMethod, $route, $handler)
{
$route = $this->currentGroupPrefix . $route;
$routeDatas = $this->routeParser->parse($route);
$routeDatas = array_reverse($this->routeParser->parse($route));
foreach ((array) $httpMethod as $method) {
foreach ($routeDatas as $routeData) {
$this->dataGenerator->addRoute($method, $routeData, $handler);
Expand Down
24 changes: 24 additions & 0 deletions test/Dispatcher/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,30 @@ public function provideFoundDispatchCases()

$cases[] = ['POST', '/bar', $callback, 'handler1', ['foo' => 'bar']];

// 27 -------------------------------------------------------------------------------------->

$callback = function (RouteCollector $r) {
$r->addRoute('GET', '/about[/{aboutwhat}[/location]]', 'handler0');
};

$cases[] = ['GET', '/about/some/location', $callback, 'handler0', [
'aboutwhat' => 'some',
], [
'_route' => '/about[/{aboutwhat}[/location]]',
]];

// 28 -------------------------------------------------------------------------------------->

$callback = function (RouteCollector $r) {
$r->addRoute('GET', '/about[/{aboutwhat:.*}[/location]]', 'handler0');
};

$cases[] = ['GET', '/about/the/nested/location', $callback, 'handler0', [
'aboutwhat' => 'the/nested',
], [
'_route' => '/about[/{aboutwhat:.*}[/location]]',
]];

// x -------------------------------------------------------------------------------------->

return $cases;
Expand Down
19 changes: 19 additions & 0 deletions test/RouteCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,25 @@ public function testGroups()

$this->assertSame($expected, $r->routes);
}

public function testOptionalRoutesCanBeUsed(): void
{
$r = new DummyRouteCollector();

$r->head('/head[/{optional}]', 'headHandler');
$r->get('/get[/{optional}/hello]', 'getHandler');
$r->post('/post[/{optional:.*}]', 'postHandler');
$r->delete('/delete[/{optional:.*}/hello]', 'deleteHandler');

$expected = [
['HEAD', '/head[/{optional}]', 'headHandler'],
['GET', '/get[/{optional}/hello]', 'getHandler'],
['POST', '/post[/{optional:.*}]', 'postHandler'],
['DELETE', '/delete[/{optional:.*}/hello]', 'deleteHandler'],
];

self::assertSame($expected, $r->routes);
}
}

class DummyRouteCollector extends RouteCollector
Expand Down