diff --git a/lib/Cake/Error/ExceptionRenderer.php b/lib/Cake/Error/ExceptionRenderer.php index 921a22094..d5f34bc2f 100644 --- a/lib/Cake/Error/ExceptionRenderer.php +++ b/lib/Cake/Error/ExceptionRenderer.php @@ -173,7 +173,7 @@ class ExceptionRenderer { * @return void */ protected function _cakeError(CakeException $error) { - $url = Router::normalize($this->controller->request->here); + $url = $this->controller->request->here(); $code = $error->getCode(); $this->controller->response->statusCode($code); $this->controller->set(array( @@ -196,7 +196,7 @@ class ExceptionRenderer { if (Configure::read('debug') == 0 && $error instanceof CakeException) { $message = __('Not Found'); } - $url = Router::normalize($this->controller->request->here); + $url = $this->controller->request->here(); $this->controller->response->statusCode($error->getCode()); $this->controller->set(array( 'name' => $message, @@ -212,7 +212,7 @@ class ExceptionRenderer { * @param array $params Parameters for controller */ public function error500($error) { - $url = Router::normalize($this->controller->request->here); + $url = $this->controller->request->here(); $code = ($error->getCode() > 500) ? $error->getCode() : 500; $this->controller->response->statusCode($code); $this->controller->set(array( diff --git a/lib/Cake/Network/CakeRequest.php b/lib/Cake/Network/CakeRequest.php index c122f43a8..8963f0e7a 100644 --- a/lib/Cake/Network/CakeRequest.php +++ b/lib/Cake/Network/CakeRequest.php @@ -500,6 +500,23 @@ class CakeRequest implements ArrayAccess { return $this; } +/** + * Get the value of the current requests url. Will include named parameters and querystring arguments. + * + * @param boolean $base Include the base path, set to false to trim the base path off. + * @return string the current request url including query string args. + */ + public function here($base = true) { + $url = $this->here; + if (!empty($this->query)) { + $url .= '?' . http_build_query($this->query); + } + if (!$base) { + $url = preg_replace('/^' . preg_quote($this->base, '/') . '/', '', $url, 1); + } + return $url; + } + /** * Read an HTTP header from the Request information. * @@ -535,7 +552,8 @@ class CakeRequest implements ArrayAccess { /** * Get the domain name and include $tldLength segments of the tld. * - * @param int $tldLength Number of segments your tld contains + * @param int $tldLength Number of segments your tld contains. For example: `example.com` contains 1 tld. + * While `example.co.uk` contains 2. * @return string Domain name without subdomains. */ public function domain($tldLength = 1) { @@ -547,7 +565,8 @@ class CakeRequest implements ArrayAccess { /** * Get the subdomains for a host. * - * @param int $tldLength Number of segments your tld contains. + * @param int $tldLength Number of segments your tld contains. For example: `example.com` contains 1 tld. + * While `example.co.uk` contains 2. * @return array of subdomains. */ public function subdomains($tldLength = 1) { diff --git a/lib/Cake/Routing/Dispatcher.php b/lib/Cake/Routing/Dispatcher.php index 02e8aed2f..b37350ba9 100644 --- a/lib/Cake/Routing/Dispatcher.php +++ b/lib/Cake/Routing/Dispatcher.php @@ -40,14 +40,6 @@ App::uses('Debugger', 'Utility'); */ class Dispatcher { -/** - * Current URL - * - * @var string - * @access public - */ - public $here = false; - /** * The request object * @@ -94,9 +86,7 @@ class Dispatcher { * are encountered. */ public function dispatch(CakeRequest $request, $additionalParams = array()) { - $this->here = $request->here; - - if ($this->asset($request->url) || $this->cached($request->url)) { + if ($this->asset($request->url) || $this->cached($request->here)) { return; } @@ -264,12 +254,11 @@ class Dispatcher { /** * Outputs cached dispatch view cache * - * @param string $url Requested URL + * @param string $path Requested URL path */ - public function cached($url) { + public function cached($path) { if (Configure::read('Cache.check') === true) { - $path = $this->here; - if ($this->here == '/') { + if ($path == '/') { $path = 'home'; } $path = strtolower(Inflector::slug($path)); diff --git a/lib/Cake/Routing/Route/CakeRoute.php b/lib/Cake/Routing/Route/CakeRoute.php index 798d26c92..ca96428ba 100644 --- a/lib/Cake/Routing/Route/CakeRoute.php +++ b/lib/Cake/Routing/Route/CakeRoute.php @@ -193,12 +193,13 @@ class CakeRoute { } else { $header = 'http_' . $header[1]; } + $header = strtoupper($header); $val = (array)$val; $h = false; foreach ($val as $v) { - if (env(strtoupper($header)) === $v) { + if (env($header) === $v) { $h = true; } } diff --git a/lib/Cake/View/Helper/CacheHelper.php b/lib/Cake/View/Helper/CacheHelper.php index fd3321fce..5bd804474 100644 --- a/lib/Cake/View/Helper/CacheHelper.php +++ b/lib/Cake/View/Helper/CacheHelper.php @@ -215,8 +215,8 @@ class CacheHelper extends AppHelper { } else { $cacheTime = strtotime($timestamp, $now); } - $path = $this->request->here; - if ($this->here == '/') { + $path = $this->request->here(); + if ($path == '/') { $path = 'home'; } $cache = strtolower(Inflector::slug($path)); diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index ea336fca5..17dd064c5 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -241,7 +241,7 @@ class FormHelper extends AppHelper { $this->_inputDefaults = $options['inputDefaults']; unset($options['inputDefaults']); if ($options['action'] === null && $options['url'] === null) { - $options['action'] = $this->request->here; + $options['action'] = $this->request->here(false); if (!isset($options['id'])) { $options['id'] = $this->domId($this->request['action'] . 'Form'); } diff --git a/lib/Cake/tests/cases/libs/cake_request.test.php b/lib/Cake/tests/cases/libs/cake_request.test.php index 0e5c926c2..65eb45042 100644 --- a/lib/Cake/tests/cases/libs/cake_request.test.php +++ b/lib/Cake/tests/cases/libs/cake_request.test.php @@ -1402,6 +1402,30 @@ class CakeRequestTestCase extends CakeTestCase { $this->assertFalse($result); } +/** + * test the here() method + * + * @return void + */ + function testHere() { + Configure::write('App.base', '/base_path'); + $_GET = array('test' => 'value'); + $request = new CakeRequest('/posts/add/1/name:value'); + + $result = $request->here(); + $this->assertEquals('/base_path/posts/add/1/name:value?test=value', $result); + + $result = $request->here(false); + $this->assertEquals('/posts/add/1/name:value?test=value', $result); + + $request = new CakeRequest('/posts/base_path/1/name:value'); + $result = $request->here(); + $this->assertEquals('/base_path/posts/base_path/1/name:value?test=value', $result); + + $result = $request->here(false); + $this->assertEquals('/posts/base_path/1/name:value?test=value', $result); + } + /** * loadEnvironment method * diff --git a/lib/Cake/tests/cases/libs/dispatcher.test.php b/lib/Cake/tests/cases/libs/dispatcher.test.php index c9d21c9d4..b3f12d974 100644 --- a/lib/Cake/tests/cases/libs/dispatcher.test.php +++ b/lib/Cake/tests/cases/libs/dispatcher.test.php @@ -1424,7 +1424,7 @@ class DispatcherTest extends CakeTestCase { $out = ob_get_clean(); ob_start(); - $dispatcher->cached($request); + $dispatcher->cached($request->here); $cached = ob_get_clean(); $result = str_replace(array("\t", "\r\n", "\n"), "", $out); @@ -1433,7 +1433,7 @@ class DispatcherTest extends CakeTestCase { $this->assertEqual($result, $expected); - $filename = $this->__cachePath($dispatcher->here); + $filename = $this->__cachePath($request->here); unlink($filename); $request = new CakeRequest('test_cached_pages/index'); @@ -1446,7 +1446,7 @@ class DispatcherTest extends CakeTestCase { $out = ob_get_clean(); ob_start(); - $dispatcher->cached($request); + $dispatcher->cached($request->here); $cached = ob_get_clean(); $result = str_replace(array("\t", "\r\n", "\n"), "", $out); @@ -1454,7 +1454,7 @@ class DispatcherTest extends CakeTestCase { $expected = str_replace(array("\t", "\r\n", "\n"), "", $cached); $this->assertEqual($result, $expected); - $filename = $this->__cachePath($dispatcher->here); + $filename = $this->__cachePath($request->here); unlink($filename); $request = new CakeRequest('TestCachedPages/index'); @@ -1464,7 +1464,7 @@ class DispatcherTest extends CakeTestCase { $out = ob_get_clean(); ob_start(); - $dispatcher->cached($request); + $dispatcher->cached($request->here); $cached = ob_get_clean(); $result = str_replace(array("\t", "\r\n", "\n"), "", $out); @@ -1472,7 +1472,7 @@ class DispatcherTest extends CakeTestCase { $expected = str_replace(array("\t", "\r\n", "\n"), "", $cached); $this->assertEqual($result, $expected); - $filename = $this->__cachePath($dispatcher->here); + $filename = $this->__cachePath($request->here); unlink($filename); $request = new CakeRequest('TestCachedPages/test_nocache_tags'); @@ -1482,7 +1482,7 @@ class DispatcherTest extends CakeTestCase { $out = ob_get_clean(); ob_start(); - $dispatcher->cached($request); + $dispatcher->cached($request->here); $cached = ob_get_clean(); $result = str_replace(array("\t", "\r\n", "\n"), "", $out); @@ -1490,7 +1490,7 @@ class DispatcherTest extends CakeTestCase { $expected = str_replace(array("\t", "\r\n", "\n"), "", $cached); $this->assertEqual($result, $expected); - $filename = $this->__cachePath($dispatcher->here); + $filename = $this->__cachePath($request->here); unlink($filename); $request = new CakeRequest('test_cached_pages/view/param/param'); @@ -1500,7 +1500,7 @@ class DispatcherTest extends CakeTestCase { $out = ob_get_clean(); ob_start(); - $dispatcher->cached($request); + $dispatcher->cached($request->here); $cached = ob_get_clean(); $result = str_replace(array("\t", "\r\n", "\n"), "", $out); @@ -1508,7 +1508,7 @@ class DispatcherTest extends CakeTestCase { $expected = str_replace(array("\t", "\r\n", "\n"), "", $cached); $this->assertEqual($result, $expected); - $filename = $this->__cachePath($dispatcher->here); + $filename = $this->__cachePath($request->here); unlink($filename); $request = new CakeRequest('test_cached_pages/view/foo:bar/value:goo'); @@ -1518,7 +1518,7 @@ class DispatcherTest extends CakeTestCase { $out = ob_get_clean(); ob_start(); - $dispatcher->cached($request); + $dispatcher->cached($request->here); $cached = ob_get_clean(); $result = str_replace(array("\t", "\r\n", "\n"), "", $out); @@ -1526,7 +1526,7 @@ class DispatcherTest extends CakeTestCase { $expected = str_replace(array("\t", "\r\n", "\n"), "", $cached); $this->assertEqual($result, $expected); - $filename = $this->__cachePath($dispatcher->here); + $filename = $this->__cachePath($request->here); $this->assertTrue(file_exists($filename)); unlink($filename); diff --git a/lib/Cake/tests/cases/libs/view/helpers/form.test.php b/lib/Cake/tests/cases/libs/view/helpers/form.test.php index c4f4f6dda..51e09d85d 100644 --- a/lib/Cake/tests/cases/libs/view/helpers/form.test.php +++ b/lib/Cake/tests/cases/libs/view/helpers/form.test.php @@ -668,12 +668,13 @@ class FormHelperTest extends CakeTestCase { function setUp() { parent::setUp(); + Configure::write('App.base', ''); $this->Controller = new ContactTestController(); $this->View = new View($this->Controller); $this->Form = new FormHelper($this->View); $this->Form->Html = new HtmlHelper($this->View); - $this->Form->request = new CakeRequest(null, false); + $this->Form->request = new CakeRequest('contacts/add', false); $this->Form->request->here = '/contacts/add'; $this->Form->request['action'] = 'add'; $this->Form->request->webroot = ''; @@ -5670,12 +5671,21 @@ class FormHelperTest extends CakeTestCase { '/div' ); $this->assertTags($result, $expected); + } - $this->Form->request->here = '/contacts/add/Contact:1'; - $result = $this->Form->create(); +/** + * test create() with automatic url generation + * + * @return void + */ + function testCreateAutoUrl() { + Router::setRequestInfo(array(array(), array('base' => '/base_url'))); + $this->Form->request->here = '/base_url/contacts/add/Contact:1'; + $this->Form->request->base = '/base_url'; + $result = $this->Form->create('Contact'); $expected = array( 'form' => array( - 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add/Contact:1', + 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1', 'accept-charset' => 'utf-8' ), 'div' => array('style' => 'display:none;'), @@ -5685,11 +5695,12 @@ class FormHelperTest extends CakeTestCase { $this->assertTags($result, $expected); $this->Form->request['action'] = 'delete'; - $this->Form->request->here = '/contacts/delete/10/User:42'; - $result = $this->Form->create(); + $this->Form->request->here = '/base_url/contacts/delete/10/User:42'; + $this->Form->request->base = '/base_url'; + $result = $this->Form->create('Contact'); $expected = array( 'form' => array( - 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/contacts/delete/10/User:42', + 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42', 'accept-charset' => 'utf-8' ), 'div' => array('style' => 'display:none;'),