Skip to content

Commit

Permalink
Merge pull request #12575 from cakephp/issue-12572
Browse files Browse the repository at this point in the history
Fix entity route incorrectly handling placeholders
  • Loading branch information
markstory committed Sep 21, 2018
2 parents ae7ec50 + b85672a commit 946e7db
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Routing/Route/EntityRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ class EntityRoute extends Route
*/
public function match(array $url, array $context = [])
{
if (empty($this->_compiledRoute)) {
$this->compile();
}

if (isset($url['_entity'])) {
$entity = $url['_entity'];
$this->_checkEntity($entity);

preg_match_all('@:(\w+)@', $this->template, $matches);

foreach ($matches[1] as $field) {
foreach ($this->keys as $field) {
if (!isset($url[$field]) && isset($entity[$field])) {
$url[$field] = $entity[$field];
}
Expand Down
27 changes: 27 additions & 0 deletions tests/TestCase/Routing/Route/EntityRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,33 @@ public function testMatchEntityObject()
$this->assertEquals('/articles/2/article-slug', $result);
}

/**
* test that routes match their pattern.
*
* @return void
*/
public function testMatchUnderscoreBetweenVar()
{
$entity = new Article([
'category_id' => 2,
'slug' => 'article-slug'
]);

$route = $route = new EntityRoute(
'/articles/:category_id_:slug',
[
'_name' => 'articlesView',
]
);

$result = $route->match([
'_entity' => $entity,
'_name' => 'articlesView'
]);

$this->assertEquals('/articles/2_article-slug', $result);
}

/**
* test that routes match their pattern.
*
Expand Down

0 comments on commit 946e7db

Please sign in to comment.