Skip to content
Permalink
Browse files

Adding methods to get host, domain and subdomains for a request.

Tests added.
  • Loading branch information...
markstory committed Aug 22, 2010
1 parent 0ec0962 commit ec22db597f70d914e0f815df2ca9b7a02abd973d
Showing with 77 additions and 0 deletions.
  1. +32 −0 cake/libs/cake_request.php
  2. +45 −0 cake/tests/cases/libs/cake_request.test.php
@@ -561,6 +561,38 @@ public function method() {
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.
@@ -512,6 +512,51 @@ function testMethod() {
$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
*

0 comments on commit ec22db5

Please sign in to comment.
You can’t perform that action at this time.