Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Supoprt query parameters on Router::parse. It will increase the symme…

…try of parse and reverse methods
  • Loading branch information...
commit ce5563ff830021b2072e4d596360c10986b3ddd9 1 parent 79ad8ab
@jrbasso jrbasso authored
View
7 lib/Cake/Routing/Router.php
@@ -551,7 +551,8 @@ public static function parse($url) {
$url = '/' . $url;
}
if (strpos($url, '?') !== false) {
- $url = substr($url, 0, strpos($url, '?'));
+ list($url, $queryParameters) = explode('?', $url, 2);
+ parse_str($queryParameters, $queryParameters);
}
extract(self::_parseExtension($url));
@@ -572,6 +573,10 @@ public static function parse($url) {
if (!empty($ext) && !isset($out['ext'])) {
$out['ext'] = $ext;
}
+
+ if (!empty($queryParameters) && !isset($out['?'])) {
+ $out['?'] = $queryParameters;
+ }
return $out;
}
View
37 lib/Cake/Test/Case/Routing/RouterTest.php
@@ -1053,6 +1053,12 @@ public function testUrlParsing() {
$result = Router::parse('/posts/view/foo:bar/routing:fun/answer:42');
$expected = array('pass' => array(), 'named' => array('foo' => 'bar', 'routing' => 'fun', 'answer' => '42'), 'plugin' => null, 'controller' => 'posts', 'action' => 'view');
$this->assertEquals($expected, $result);
+
+ Router::reload();
+ Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('named' => array('foo', 'answer'), 'greedyNamed' => true));
+ $result = Router::parse('/posts/view/foo:bar/routing:fun/answer:42?id=123&tab=abc');
+ $expected = array('pass' => array(), 'named' => array('foo' => 'bar', 'routing' => 'fun', 'answer' => '42'), 'plugin' => null, 'controller' => 'posts', 'action' => 'view', '?' => array('id' => '123', 'tab' => 'abc'));
+ $this->assertEquals($expected, $result);
}
/**
@@ -1170,6 +1176,33 @@ public function testRouteSymmetry() {
}
/**
+ * Test parse and reverse symmetry
+ *
+ * @return void
+ * @dataProvider parseReverseSymmetryData
+ */
+ public function testParseReverseSymmetry($url) {
+ $this->assertSame($url, Router::reverse(Router::parse($url) + array('url' => array())));
+ }
+
+/**
+ * Data for parse and reverse test
+ *
+ * @return array
+ */
+ public function parseReverseSymmetryData() {
+ return array(
+ array('/'),
+ array('/controller/action'),
+ array('/controller/action/param'),
+ array('/controller/action?param1=value1&param2=value2'),
+ array('/controller/action/param?param1=value1'),
+ array('/controller/action/named1:nv1'),
+ array('/controller/action/named1:nv1?param1=value1')
+ );
+ }
+
+/**
* Test that Routing.prefixes are used when a Router instance is created
* or reset
*
@@ -1319,9 +1352,11 @@ public function testExtensionParsing() {
$this->assertEquals($expected, $result);
$result = Router::parse('/posts/view/1.rss?query=test');
+ $expected['?'] = array('query' => 'test');
$this->assertEquals($expected, $result);
$result = Router::parse('/posts/view/1.atom');
+ unset($expected['?']);
$expected['ext'] = 'atom';
$this->assertEquals($expected, $result);
@@ -1335,7 +1370,7 @@ public function testExtensionParsing() {
$this->assertEquals($expected, $result);
$result = Router::parse('/posts.atom?hello=goodbye');
- $expected = array('plugin' => null, 'controller' => 'posts.atom', 'action' => 'index', 'pass' => array(), 'named' => array());
+ $expected = array('plugin' => null, 'controller' => 'posts.atom', 'action' => 'index', 'pass' => array(), 'named' => array(), '?' => array('hello' => 'goodbye'));
$this->assertEquals($expected, $result);
Router::reload();

0 comments on commit ce5563f

Please sign in to comment.
Something went wrong with that request. Please try again.