From ec22db597f70d914e0f815df2ca9b7a02abd973d Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 22 Aug 2010 00:44:05 -0400 Subject: [PATCH] Adding methods to get host, domain and subdomains for a request. Tests added. --- cake/libs/cake_request.php | 32 +++++++++++++++ cake/tests/cases/libs/cake_request.test.php | 45 +++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/cake/libs/cake_request.php b/cake/libs/cake_request.php index 1b15c2239..d3828e15b 100644 --- a/cake/libs/cake_request.php +++ b/cake/libs/cake_request.php @@ -561,6 +561,38 @@ class CakeRequest implements ArrayAccess { return env('REQUEST_METHOD'); } +/** + * Get the host that the request was handled on. + * + * @return void + */ + public function host() { + return env('HTTP_HOST'); + } + +/** + * Get the domain name and include $tldLength segments of the tld. + * + * @param int $tldLength Number of segments your tld contains + * @return string Domain name without subdomains. + */ + function domain($tldLength = 1) { + $segments = explode('.', $this->host()); + $domain = array_slice($segments, -1 * ($tldLength + 1)); + return implode('.', $domain); + } + +/** + * Get the subdomains for a host. + * + * @param int $tldLength Number of segments your tld contains. + * @return array of subdomains. + */ + function subdomains($tldLength = 1) { + $segments = explode('.', $this->host()); + return array_slice($segments, 0, -1 * ($tldLength + 1)); + } + /** * Find out which content types the client accepts or check if they accept a * particular type of content. diff --git a/cake/tests/cases/libs/cake_request.test.php b/cake/tests/cases/libs/cake_request.test.php index ff7107a56..17089b4cb 100644 --- a/cake/tests/cases/libs/cake_request.test.php +++ b/cake/tests/cases/libs/cake_request.test.php @@ -512,6 +512,51 @@ class CakeRequestTestCase extends CakeTestCase { $this->assertEquals('delete', $request->method()); } +/** + * test host retrieval. + * + * @return void + */ + function testHost() { + $_SERVER['HTTP_HOST'] = 'localhost'; + $request = new CakeRequest('some/path'); + + $this->assertEquals('localhost', $request->host()); + } + +/** + * test domain retrieval. + * + * @return void + */ + function testDomain() { + $_SERVER['HTTP_HOST'] = 'something.example.com'; + $request = new CakeRequest('some/path'); + + $this->assertEquals('example.com', $request->domain()); + + $_SERVER['HTTP_HOST'] = 'something.example.co.uk'; + $this->assertEquals('example.co.uk', $request->domain(2)); + } + +/** + * test getting subdomains for a host. + * + * @return void + */ + function testSubdomain() { + $_SERVER['HTTP_HOST'] = 'something.example.com'; + $request = new CakeRequest('some/path'); + + $this->assertEquals(array('something'), $request->subdomains()); + + $_SERVER['HTTP_HOST'] = 'www.something.example.com'; + $this->assertEquals(array('www', 'something'), $request->subdomains()); + + $_SERVER['HTTP_HOST'] = 'www.something.example.co.uk'; + $this->assertEquals(array('www', 'something'), $request->subdomains(2)); + } + /** * test ajax, flash and friends *