Skip to content

Commit

Permalink
Adding support for route objects to return full urls. These urls are …
Browse files Browse the repository at this point in the history
…not changed by Router, and just returned. This will allow subdomain route classes to be created.

Tests added.
  • Loading branch information
markstory committed Oct 30, 2010
1 parent 14ec870 commit 3ca0cdb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
29 changes: 14 additions & 15 deletions cake/libs/router.php
Expand Up @@ -736,8 +736,10 @@ public static function url($url = null, $full = false) {

$base = $path['base'];
$extension = $output = $mapped = $q = $frag = null;

if (is_array($url)) {

if (empty($url)) {
return isset($path['here']) ? $path['here'] : '/';
} elseif (is_array($url)) {
if (isset($url['base']) && $url['base'] === false) {
$base = null;
unset($url['base']);
Expand Down Expand Up @@ -798,20 +800,13 @@ public static function url($url = null, $full = false) {
if ($match === false) {
$output = self::_handleNoRoute($url);
}
$output = str_replace('//', '/', $base . '/' . $output);
} else {
if (((strpos($url, '://')) || (strpos($url, 'javascript:') === 0) || (strpos($url, 'mailto:') === 0)) || (!strncmp($url, '#', 1))) {
return $url;
}
if (empty($url)) {
if (!isset($path['here'])) {
$path['here'] = '/';
}
$output = $path['here'];
} elseif (substr($url, 0, 1) === '/') {
$output = $base . $url;
if (substr($url, 0, 1) === '/') {
$output = substr($url, 1);
} else {
$output = $base . '/';
foreach (self::$_prefixes as $prefix) {
if (isset($params[$prefix])) {
$output .= $prefix . '/';
Expand All @@ -823,15 +818,19 @@ public static function url($url = null, $full = false) {
}
$output .= Inflector::underscore($params['controller']) . '/' . $url;
}
$output = str_replace('//', '/', $output);
}
$protocol = strpos($output, '://');
if ($protocol > 3 && $protocol < 6) {
return $output . $extension . self::queryString($q, array(), $escape) . $frag;
}
$output = str_replace('//', '/', $base . '/' . $output);

if ($full && defined('FULL_BASE_URL')) {
$output = FULL_BASE_URL . $output;
}
if (!empty($extension) && substr($output, -1) === '/') {
$output = substr($output, 0, -1);
if (!empty($extension)) {
$output = rtrim($output, '/');
}

return $output . $extension . self::queryString($q, array(), $escape) . $frag;
}

Expand Down
18 changes: 18 additions & 0 deletions cake/tests/cases/libs/router.test.php
Expand Up @@ -2191,4 +2191,22 @@ function testSetRequestInfoLegacy() {
$this->assertEqual($result->here, '/protected/images/index');
$this->assertEqual($result->webroot, '/');
}

/**
* test that a route object returning a full url is not modified.
*
* @return void
*/
function testUrlFullUrlReturnFromRoute() {
$url = 'http://example.com/posts/view/1';

$this->getMock('CakeRoute', array(), array('/'), 'MockReturnRoute');
$routes = Router::connect('/:controller/:action', array(), array('routeClass' => 'MockReturnRoute'));
$routes[0]->expects($this->any())->method('match')
->will($this->returnValue($url));

$result = Router::url(array('controller' => 'posts', 'action' => 'view', 1));
$this->assertEquals($url, $result);
}

}

0 comments on commit 3ca0cdb

Please sign in to comment.