From 0dad565fce54362bdccd4c3b00d6a784cdb32192 Mon Sep 17 00:00:00 2001 From: Mackenzie Carr Date: Wed, 11 Feb 2015 17:27:29 -0400 Subject: [PATCH 1/4] [5.1] Fix Domain wildcard group in routes and url generation for named routes A proposed solution for issue #7366. For the route method if there are no parameters, get the parameters from the current request to correctly parse a URL. ~~~ Route::group( [ 'domain' => '{subdomain}.domain.com', ], function ($router) { Route::get( '/', [ 'uses' => function ($subdomain) { return route('subhome'); }, 'as' => 'subhome' ] ); } ); ~~~ Currently this would generate: ~~~ http://%7Bsubdomain%7D.domain.com ~~~ By getting the parameters from the current request it now generates this: ~~~ http://blah.domain.com ~~~ Not sure if this is the best "solution" just thought I would share :) --- src/Illuminate/Routing/UrlGenerator.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Routing/UrlGenerator.php b/src/Illuminate/Routing/UrlGenerator.php index dac9c06b257b..4362454a7cf8 100755 --- a/src/Illuminate/Routing/UrlGenerator.php +++ b/src/Illuminate/Routing/UrlGenerator.php @@ -246,9 +246,13 @@ public function route($name, $parameters = array(), $absolute = true) { if ( ! is_null($route = $this->routes->getByName($name))) { - return $this->toRoute($route, $parameters, $absolute); + if (count($parameters)) { + return $this->toRoute($route, $parameters, $absolute); + } else { + $parameters = $route->bind($this->getRequest())->parameters(); + return $this->toRoute($route, $parameters, $absolute); + } } - throw new InvalidArgumentException("Route [{$name}] not defined."); } From fbcc69f44e24735d7a1b9ea396ead122df791b10 Mon Sep 17 00:00:00 2001 From: Mackenzie Carr Date: Wed, 11 Feb 2015 17:51:49 -0400 Subject: [PATCH 2/4] Added test --- tests/Routing/RoutingUrlGeneratorTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/Routing/RoutingUrlGeneratorTest.php b/tests/Routing/RoutingUrlGeneratorTest.php index c1a63b507858..176d516e5b00 100755 --- a/tests/Routing/RoutingUrlGeneratorTest.php +++ b/tests/Routing/RoutingUrlGeneratorTest.php @@ -248,6 +248,20 @@ public function testRoutesWithDomainsAndPorts() $this->assertEquals('http://sub.taylor.com:8080/foo/bar/otwell', $url->route('bar', array('taylor', 'otwell'))); } + public function testRoutesWithDomainWildcard() + { + $url = new UrlGenerator( + $routes = new Illuminate\Routing\RouteCollection, + $request = Illuminate\Http\Request::create('http://sub.foo.com') + ); + + $route = new Illuminate\Routing\Route(array('GET'), '/', array('as' => 'bar', 'domain' => '{subdomain}.foo.com')); + $routes->add($route); + + //Parameters filled by current request + $this->assertEquals('http://sub.foo.com', $url->route('bar')); + } + public function testHttpsRoutesWithDomains() { From 72af15fdecc50ddc74bf503ca31ce85355b49a36 Mon Sep 17 00:00:00 2001 From: Mackenzie Carr Date: Wed, 11 Feb 2015 18:15:23 -0400 Subject: [PATCH 3/4] Refactored --- src/Illuminate/Routing/UrlGenerator.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Routing/UrlGenerator.php b/src/Illuminate/Routing/UrlGenerator.php index 4362454a7cf8..29ba960ce8fa 100755 --- a/src/Illuminate/Routing/UrlGenerator.php +++ b/src/Illuminate/Routing/UrlGenerator.php @@ -246,12 +246,11 @@ public function route($name, $parameters = array(), $absolute = true) { if ( ! is_null($route = $this->routes->getByName($name))) { - if (count($parameters)) { - return $this->toRoute($route, $parameters, $absolute); - } else { - $parameters = $route->bind($this->getRequest())->parameters(); - return $this->toRoute($route, $parameters, $absolute); + if (count($parameters) >= 1) { + return $this->toRoute($route, $parameters, $absolute); } + + return $this->toRoute($route, $route->bind($this->getRequest())->parameters(), $absolute); } throw new InvalidArgumentException("Route [{$name}] not defined."); } From 7b7bc3cc9e3541129322f47f9633f361357f12dd Mon Sep 17 00:00:00 2001 From: Mackenzie Carr Date: Wed, 11 Feb 2015 18:30:54 -0400 Subject: [PATCH 4/4] Fixed spacing --- src/Illuminate/Routing/UrlGenerator.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Illuminate/Routing/UrlGenerator.php b/src/Illuminate/Routing/UrlGenerator.php index 29ba960ce8fa..42bba4ed77ce 100755 --- a/src/Illuminate/Routing/UrlGenerator.php +++ b/src/Illuminate/Routing/UrlGenerator.php @@ -244,15 +244,16 @@ public function forceSchema($schema) */ public function route($name, $parameters = array(), $absolute = true) { - if ( ! is_null($route = $this->routes->getByName($name))) + if (is_null($route = $this->routes->getByName($name))) { - if (count($parameters) >= 1) { - return $this->toRoute($route, $parameters, $absolute); - } - - return $this->toRoute($route, $route->bind($this->getRequest())->parameters(), $absolute); + throw new InvalidArgumentException("Route [{$name}] not defined."); + } + + if (count($parameters) > 0) { + return $this->toRoute($route, $parameters, $absolute); } - throw new InvalidArgumentException("Route [{$name}] not defined."); + + return $this->toRoute($route, $route->bind($this->getRequest())->parameters(), $absolute); } /**