From eb8dddf850495d210890c09aa53605383d08420a Mon Sep 17 00:00:00 2001 From: Pavel Puchkin Date: Mon, 6 May 2013 16:31:21 +1100 Subject: [PATCH] Use dashes instead of underscores when generating URLs to controller routes As mentioned [here](http://forums.laravel.io/viewtopic.php?id=539), it is better to replace underscores with dashes in automatically generated URLs. Tests included. --- laravel/routing/controller.php | 2 +- laravel/routing/router.php | 2 +- laravel/tests/application/controllers/auth.php | 7 ++++++- laravel/tests/cases/controller.test.php | 8 +------- laravel/tests/cases/routing.test.php | 6 ++++-- laravel/tests/cases/url.test.php | 6 +++++- laravel/url.php | 3 ++- 7 files changed, 20 insertions(+), 14 deletions(-) diff --git a/laravel/routing/controller.php b/laravel/routing/controller.php index 4e24494876d..d6e364339e7 100644 --- a/laravel/routing/controller.php +++ b/laravel/routing/controller.php @@ -183,7 +183,7 @@ protected static function references(&$destination, &$parameters) $search = '(:'.($key + 1).')'; - $destination = str_replace($search, $value, $destination, $count); + $destination = str_replace($search, str_replace('-', '_', $value), $destination, $count); if ($count > 0) unset($parameters[$key]); } diff --git a/laravel/routing/router.php b/laravel/routing/router.php index 36e6b71195b..0f776657282 100644 --- a/laravel/routing/router.php +++ b/laravel/routing/router.php @@ -212,7 +212,7 @@ public static function register($method, $route, $action) $uri = ltrim(str_replace('(:bundle)', static::$bundle, $uri), '/'); - if($uri == '') + if ($uri == '') { $uri = '/'; } diff --git a/laravel/tests/application/controllers/auth.php b/laravel/tests/application/controllers/auth.php index 4e712ed150b..0ae36582522 100644 --- a/laravel/tests/application/controllers/auth.php +++ b/laravel/tests/application/controllers/auth.php @@ -12,9 +12,14 @@ public function action_login() return __FUNCTION__; } + public function action_dashes_in_the_name() + { + return __FUNCTION__; + } + public function action_profile($name) { return $name; } -} \ No newline at end of file +} diff --git a/laravel/tests/cases/controller.test.php b/laravel/tests/cases/controller.test.php index 9eb192b9165..8ee12e7002e 100644 --- a/laravel/tests/cases/controller.test.php +++ b/laravel/tests/cases/controller.test.php @@ -184,18 +184,12 @@ public function testMultipleFiltersCanBeAssignedToAnAction() public function testRestfulControllersRespondWithRestfulMethods() { Request::$foundation->setMethod('GET'); - //$_SERVER['REQUEST_METHOD'] = 'GET'; - $this->assertEquals('get_index', Controller::call('restful@index')->content); - //$_SERVER['REQUEST_METHOD'] = 'PUT'; Request::$foundation->setMethod('PUT'); - $this->assertEquals(404, Controller::call('restful@index')->status()); - //$_SERVER['REQUEST_METHOD'] = 'POST'; Request::$foundation->setMethod('POST'); - $this->assertEquals('post_index', Controller::call('restful@index')->content); } @@ -264,4 +258,4 @@ public function testResolveMethodChecksTheIoCContainer() $this->assertEquals('bar', $controller->foo); } -} \ No newline at end of file +} diff --git a/laravel/tests/cases/routing.test.php b/laravel/tests/cases/routing.test.php index 68a112a0763..d56b1c61432 100644 --- a/laravel/tests/cases/routing.test.php +++ b/laravel/tests/cases/routing.test.php @@ -5,7 +5,7 @@ class RoutingTest extends PHPUnit_Framework_TestCase { /** - * Destroy the testing environment. + * Initialize the test environment. */ public function setUp() { @@ -115,6 +115,8 @@ public function testBasicRouteToControllerIsRouted() $this->assertEquals('home@(:1)', Router::route('GET', 'home/profile')->action['uses']); $this->assertEquals('admin.panel@(:1)', Router::route('GET', 'admin/panel')->action['uses']); $this->assertEquals('admin.panel@(:1)', Router::route('GET', 'admin/panel/show')->action['uses']); + + $this->assertEquals('action_dashes_in_the_name', Router::route('GET', 'auth/dashes-in-the-name')->call()); } /** @@ -157,4 +159,4 @@ public function testForeignCharsInRoutes() $this->assertEquals(urlencode('私は料理が大好き'), Router::route('GET', urlencode('私は料理が大好き'))->uri); } -} \ No newline at end of file +} diff --git a/laravel/tests/cases/url.test.php b/laravel/tests/cases/url.test.php index 290a1db0273..da923155776 100644 --- a/laravel/tests/cases/url.test.php +++ b/laravel/tests/cases/url.test.php @@ -65,6 +65,10 @@ public function testToActionMethodGeneratesURLToControllerAction() $this->assertEquals('http://localhost/index.php/x/y/Taylor', URL::to_action('x@y', array('Taylor'))); $this->assertEquals('http://localhost/index.php/foo/bar', URL::to_action('foo@baz')); $this->assertEquals('http://localhost/index.php/foo/bar/Taylor', URL::to_action('foo@baz', array('Taylor'))); + + Route::controller('foo.bar'); + $this->assertEquals('http://localhost/index.php/foo/bar/index', URL::to_action('foo.bar@index')); + $this->assertEquals('http://localhost/index.php/foo/bar/some-long-action-name', URL::to_action('foo.bar@some_long_action_name')); } /** @@ -148,4 +152,4 @@ public function testUrlsGeneratedWithLanguages() Config::set('application.languages', array()); } -} \ No newline at end of file +} diff --git a/laravel/url.php b/laravel/url.php index b8fb1f4602c..8edff938d9a 100644 --- a/laravel/url.php +++ b/laravel/url.php @@ -223,7 +223,8 @@ protected static function convention($action, $parameters) // We'll replace both dots and @ signs in the URI since both are used // to specify the controller and action, and by convention should be // translated into URI slashes for the URL. - $uri = $root.'/'.str_replace(array('.', '@'), '/', $action); + // We'll also replace underscores with dashes + $uri = $root.'/'.str_replace(array('.', '@'), '/', str_replace('_', '-', $action)); $uri = static::to(str_finish($uri, '/').$parameters);