Skip to content

Commit

Permalink
Have Controller::generateRouteName() always put the method first.
Browse files Browse the repository at this point in the history
At present, mounted controllers can get weird route names. For instance, let's
say you have this controller definition:

    $otherController = $app['controllers_factory'];
    $otherController->get('/{name}', function (Request $request, $name) use ($app) {
      return new Response("Goodbye $name!\n", 200, ['Content-Type' => 'text/plain']);
      });
    $app->mount('/goodbye', $otherController);

The generated route name in this case will be `_goodbyeGET_name`, which
technically contains everything, but is ugly.

With this PR, the route name will change to `GET_goodbye_name`, which is
considerably easier to parse when debugging.
  • Loading branch information
LawnGnome committed May 1, 2015
1 parent 909c346 commit f8d6484
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
7 changes: 5 additions & 2 deletions src/Silex/Controller.php
Expand Up @@ -107,12 +107,15 @@ public function freeze()

public function generateRouteName($prefix)
{
$methods = implode('_', $this->route->getMethods());
$methods = implode('_', $this->route->getMethods()).'_';

$routeName = $prefix.$methods.$this->route->getPath();
$routeName = $methods.$prefix.$this->route->getPath();
$routeName = str_replace(array('/', ':', '|', '-'), '_', $routeName);
$routeName = preg_replace('/[^a-z0-9A-Z_.]+/', '', $routeName);

// Collapse consecutive underscores down into a single underscore.
$routeName = preg_replace('/_+/', '_', $routeName);

return $routeName;
}
}
13 changes: 7 additions & 6 deletions tests/Silex/Tests/ControllerTest.php
Expand Up @@ -80,21 +80,22 @@ public function testRun()
/**
* @dataProvider provideRouteAndExpectedRouteName
*/
public function testDefaultRouteNameGeneration(Route $route, $expectedRouteName)
public function testDefaultRouteNameGeneration(Route $route, $prefix, $expectedRouteName)
{
$controller = new Controller($route);
$controller->bind($controller->generateRouteName(''));
$controller->bind($controller->generateRouteName($prefix));

$this->assertEquals($expectedRouteName, $controller->getRouteName());
}

public function provideRouteAndExpectedRouteName()
{
return array(
array(new Route('/Invalid%Symbols#Stripped', array(), array(), array(), '', array(), array('POST')), 'POST_InvalidSymbolsStripped'),
array(new Route('/post/{id}', array(), array(), array(), '', array(), array('GET')), 'GET_post_id'),
array(new Route('/colon:pipe|dashes-escaped'), '_colon_pipe_dashes_escaped'),
array(new Route('/underscores_and.periods'), '_underscores_and.periods'),
array(new Route('/Invalid%Symbols#Stripped', array(), array(), array(), '', array(), array('POST')), '', 'POST_InvalidSymbolsStripped'),
array(new Route('/post/{id}', array(), array(), array(), '', array(), array('GET')), '', 'GET_post_id'),
array(new Route('/colon:pipe|dashes-escaped'), '', '_colon_pipe_dashes_escaped'),
array(new Route('/underscores_and.periods'), '', '_underscores_and.periods'),
array(new Route('/post/{id}', array(), array(), array(), '', array(), array('GET')), 'prefix', 'GET_prefix_post_id'),
);
}

Expand Down

0 comments on commit f8d6484

Please sign in to comment.