Skip to content

Commit 3d6ca5a

Browse files
committed
Add query strings to URL generator and fix parameter bug
1 parent 1f6d8b5 commit 3d6ca5a

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/Routing/UrlGenerator.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,17 @@ public function route($name, $parameters = array())
108108

109109
$uri = $this->app->namedRoutes[$name];
110110

111-
foreach ($parameters as $key => $value) {
112-
$uri = preg_replace('/\{'.$key.'.*?\}/', $value, $uri);
111+
$uri = preg_replace_callback('/\{(.*?)(:.*?)?\}/', function ($m) use (&$parameters) {
112+
return isset($parameters[$m[1]]) ? array_pull($parameters, $m[1]) : $m[0];
113+
}, $uri);
114+
115+
$uri = $this->to($uri, []);
116+
117+
if (! empty($parameters)) {
118+
$uri .= '?'.http_build_query($parameters);
113119
}
114120

115-
return $this->to($uri, []);
121+
return $uri;
116122
}
117123

118124
/**

tests/FullApplicationTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ public function testGeneratingUrls()
299299
$this->assertEquals('http://lumen.laravel.com/something', url('something'));
300300
$this->assertEquals('http://lumen.laravel.com/foo-bar', route('foo'));
301301
$this->assertEquals('http://lumen.laravel.com/foo-bar/1/2', route('bar', ['baz' => 1, 'boom' => 2]));
302+
$this->assertEquals('http://lumen.laravel.com/foo-bar?baz=1&boom=2', route('foo', ['baz' => 1, 'boom' => 2]));
302303
}
303304

304305

@@ -324,6 +325,7 @@ public function testGeneratingUrlsForRegexParameters()
324325
$this->assertEquals('http://lumen.laravel.com/foo-bar', route('foo'));
325326
$this->assertEquals('http://lumen.laravel.com/foo-bar/1/2', route('bar', ['baz' => 1, 'boom' => 2]));
326327
$this->assertEquals('http://lumen.laravel.com/foo-bar/1/2', route('baz', ['baz' => 1, 'boom' => 2]));
328+
$this->assertEquals('http://lumen.laravel.com/foo-bar/{baz:[0-9]+}/{boom:[0-9]+}?ba=1&bo=2', route('baz', ['ba' => 1, 'bo' => 2]));
327329
}
328330

329331

0 commit comments

Comments
 (0)