mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-02-07 12:36:25 +00:00
Adding methods to get host, domain and subdomains for a request.
Tests added.
This commit is contained in:
parent
0ec0962932
commit
ec22db597f
2 changed files with 77 additions and 0 deletions
|
@ -561,6 +561,38 @@ class CakeRequest implements ArrayAccess {
|
||||||
return env('REQUEST_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
|
* Find out which content types the client accepts or check if they accept a
|
||||||
* particular type of content.
|
* particular type of content.
|
||||||
|
|
|
@ -512,6 +512,51 @@ class CakeRequestTestCase extends CakeTestCase {
|
||||||
$this->assertEquals('delete', $request->method());
|
$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
|
* test ajax, flash and friends
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Reference in a new issue