Skip to content

Commit

Permalink
Fix missing base path on generated urls.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Oct 8, 2012
1 parent 98780c8 commit 2f2237b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 30 deletions.
25 changes: 13 additions & 12 deletions lib/Cake/Routing/Router.php
Expand Up @@ -684,25 +684,24 @@ public static function url($url = null, $options = array()) {
$hasLeadingSlash = isset($url[0]) ? $url[0] === '/' : false;
}

$params = array(
'plugin' => null,
'controller' => null,
'action' => 'index'
);
$here = $base = $output = $frag = null;

$request = static::getRequest(true);
if ($request) {
$params = $request->params;
$here = $request->here;
} else {
$params = array(
'plugin' => null,
'controller' => null,
'action' => 'index'
);
$here = null;
$base = $request->base;
}

$output = $frag = null;

if (empty($url)) {
$output = isset($here) ? $here : '/';
if ($full && defined('FULL_BASE_URL')) {
$output = FULL_BASE_URL . $output;
$output = FULL_BASE_URL . $base . $output;
}
return $output;
} elseif ($urlType === 'array') {
Expand Down Expand Up @@ -777,9 +776,11 @@ public static function url($url = null, $options = array()) {
if ($hasColonSlash || $plainString) {
return $url;
}
if ($hasLeadingSlash) {
$output = substr($url, 1);
$output = $url;
if ($hasLeadingSlash && strlen($output) > 1) {
$output = substr($output, 1);
}
$output = $base . $output;
}
$protocol = preg_match('#^[a-z][a-z0-9+-.]*\://#i', $output);
if ($protocol === 0) {
Expand Down
44 changes: 26 additions & 18 deletions lib/Cake/Test/TestCase/Routing/RouterTest.php
Expand Up @@ -65,14 +65,9 @@ public function tearDown() {
* @return void
*/
public function testFullBaseURL() {
$skip = PHP_SAPI == 'cli';
if ($skip) {
$this->markTestSkipped('Cannot validate base urls in CLI');
}
$this->assertRegExp('/^http(s)?:\/\//', Router::url('/', true));
$this->assertRegExp('/^http(s)?:\/\//', Router::url(null, true));
$this->assertRegExp('/^http(s)?:\/\//', Router::url(array('_full' => true)));
$this->assertSame(FULL_BASE_URL . '/', Router::url(array('_full' => true)));
}

/**
Expand Down Expand Up @@ -369,26 +364,39 @@ public function testUrlNormalization() {
}

/**
* test generation of basic urls.
*
* @return void
* Test generating urls with base paths.
*/
public function testUrlGenerationBasic() {
extract(Router::getNamedExpressions());

public function testUrlGenerationWithBasePath() {
Router::connect('/:controller/:action/*');
$request = new Request();
$request->addParams(array(
'action' => 'index', 'plugin' => null, 'controller' => 'subscribe', 'admin' => true
));
$request->addParams([
'action' => 'index',
'plugin' => null,
'controller' => 'subscribe',
]);
$request->base = '/magazine';
$request->here = '/magazine';
$request->here = '/magazine/';
$request->webroot = '/magazine/';
Router::setRequestInfo($request);
Router::pushRequest($request);

$result = Router::url();
$this->assertEquals('/magazine', $result);
$this->assertEquals('/magazine/', $result);

$result = Router::url('/');
$this->assertEquals('/magazine/', $result);

$result = Router::url(['controller' => 'articles', 'action' => 'view', 1]);
$this->assertEquals('/magazine/articles/view/1', $result);
}

/**
* test generation of basic urls.
*
* @return void
*/
public function testUrlGenerationBasic() {
extract(Router::getNamedExpressions());

Router::reload();

Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
$out = Router::url(array('controller' => 'pages', 'action' => 'display', 'home'));
Expand Down

0 comments on commit 2f2237b

Please sign in to comment.