From 3ba273b16d328ec79c63c13e5b97f83211c9b23f Mon Sep 17 00:00:00 2001 From: nate Date: Tue, 17 Apr 2007 07:51:01 +0000 Subject: [PATCH] Adding URL fragment (hash) support in Router::url() (Ticket #2432) git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4869 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/router.php | 10 +++++++--- cake/tests/cases/libs/router.test.php | 13 +++++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/cake/libs/router.php b/cake/libs/router.php index daec5cc46..0d4c6a607 100644 --- a/cake/libs/router.php +++ b/cake/libs/router.php @@ -443,7 +443,7 @@ class Router extends Object { } $base = $_this->stripPlugin($path['base'], $params['plugin']); - $extension = $output = $mapped = $q = null; + $extension = $output = $mapped = $q = $frag = null; if (is_array($url) && !empty($url)) { if (isset($url['full_base']) && $url['full_base'] == true) { @@ -454,6 +454,10 @@ class Router extends Object { $q = $url['?']; unset($url['?']); } + if (isset($url['#'])) { + $frag = '#' . urlencode($url['#']); + unset($url['#']); + } if (!isset($url['action'])) { if (!isset($url['controller']) || $params['controller'] == $url['controller']) { $url['action'] = $params['action']; @@ -486,7 +490,7 @@ class Router extends Object { } $named = $args = array(); - $skip = am(array_keys($_this->__mapped), array('bare', 'action', 'controller', 'plugin', 'ext', '?')); + $skip = am(array_keys($_this->__mapped), array('bare', 'action', 'controller', 'plugin', 'ext', '?', '#')); if(defined('CAKE_ADMIN')) { $skip[] = CAKE_ADMIN; } @@ -558,7 +562,7 @@ class Router extends Object { if ($full) { $output = FULL_BASE_URL . $output; } - return $output . $extension . $_this->queryString($q); + return $output . $extension . $_this->queryString($q) . $frag; } /** * Maps a URL array onto a route and returns the string result, or false if no match diff --git a/cake/tests/cases/libs/router.test.php b/cake/tests/cases/libs/router.test.php index 54dc05cb4..df6b21dff 100644 --- a/cake/tests/cases/libs/router.test.php +++ b/cake/tests/cases/libs/router.test.php @@ -101,7 +101,6 @@ class RouterTest extends UnitTestCase { } function testUrlGeneration() { - $this->router->reload(); extract($this->router->getNamedExpressions()); $this->router->connect('/', array('controller'=>'pages', 'action'=>'display', 'home')); @@ -146,6 +145,17 @@ class RouterTest extends UnitTestCase { $result = $this->router->url(array('controller' => 'posts', '0', '?' => 'var=test&var2=test2')); $this->assertEqual($result, $expected); + $result = $this->router->url(array('controller' => 'posts', '0', '?' => array('var' => 'test', 'var2' => 'test2'))); + $this->assertEqual($result, $expected); + + $result = $this->router->url(array('controller' => 'posts', '0', '?' => 'var=test&var2=test2', '#' => 'unencoded string %')); + $expected = '/posts/index/0?var=test&var2=test2#unencoded+string+%25'; + $this->assertEqual($result, $expected); + } + + function testUrlParsing() { + extract($this->router->getNamedExpressions()); + $this->router->routes = array(); $this->router->connect('/posts/:value/:somevalue/:othervalue/*', array('controller' => 'posts', 'action' => 'view'), array('value','somevalue', 'othervalue')); $result = $this->router->parse('/posts/2007/08/01/title-of-post-here'); @@ -175,7 +185,6 @@ class RouterTest extends UnitTestCase { $result = $this->router->parse('/posts/2007/08/01/title-of-post-here'); $expected = array('year' => '2007', 'month' => '08', 'day' => '01', 'controller' => 'posts', 'action' => 'view', 'plugin' =>'', 'pass' => array('0' => 'title-of-post-here')); $this->assertEqual($result, $expected); - } function testAdminRouting() {