Skip to content

Commit

Permalink
Always include default value in the generated url (see #1975)
Browse files Browse the repository at this point in the history
Description
-----------

| Q                | A
| -----------------| ---
| Fixed issues     | n/a
| Docs PR or issue | n/a

Mark `{parameters}` as important so the url generator will add the defaults to the generated urls.
See https://symfony.com/doc/current/routing.html#optional-parameters
> If you want to always include some default value in the generated URL (for example to force the generation of /blog/1 instead of /blog in the previous example) add the ! character before the parameter name: /blog/{!page}

Commits
-------

c05a832 Always include default value in the generated url
415e822 Fix tests
  • Loading branch information
bytehead committed Jul 24, 2020
1 parent b0c1cb1 commit f0aa45b
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion core-bundle/src/Routing/RouteFactory.php
Expand Up @@ -53,7 +53,7 @@ public function createRouteForPage(PageModel $pageModel, string $defaultParamete
$requirements = $config->getRequirements();

if (null === $path) {
$path = '/'.($pageModel->alias ?: $pageModel->id).'{parameters}';
$path = '/'.($pageModel->alias ?: $pageModel->id).'{!parameters}';
$defaults['parameters'] = $defaultParameters;
$requirements['parameters'] = $pageModel->requireItem ? '/.+' : '(/.+)?';
}
Expand Down
4 changes: 2 additions & 2 deletions core-bundle/tests/Routing/RouteFactoryTest.php
Expand Up @@ -79,7 +79,7 @@ public function testCreatesParameteredPageRouteIfPathIsNullWithoutRequireItem():

$route = $this->factory->createRouteForPage($pageModel, '/items/news');

$this->assertSame('/foo/bar{parameters}.baz', $route->getPath());
$this->assertSame('/foo/bar{!parameters}.baz', $route->getPath());
$this->assertSame('/items/news', $route->getDefault('parameters'));
$this->assertSame('(/.+)?', $route->getRequirement('parameters'));
}
Expand Down Expand Up @@ -110,7 +110,7 @@ public function testCreatesParameteredPageRouteIfPathIsNullWithRequireItem(): vo

$route = $this->factory->createRouteForPage($pageModel, '/items/news');

$this->assertSame('/foo/bar{parameters}.baz', $route->getPath());
$this->assertSame('/foo/bar{!parameters}.baz', $route->getPath());
$this->assertSame('/items/news', $route->getDefault('parameters'));
$this->assertSame('/.+', $route->getRequirement('parameters'));
}
Expand Down

0 comments on commit f0aa45b

Please sign in to comment.