CakeRequest::domain PHP Method

domain() public method

Get the domain name and include $tldLength segments of the tld.
public domain ( integer $tldLength = 1 ) : string
$tldLength integer 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)
    {
        $segments = explode('.', $this->host());
        $domain = array_slice($segments, -1 * ($tldLength + 1));
        return implode('.', $domain);
    }

Usage Example

 /**
  * Test domain retrieval.
  *
  * @return void
  */
 public 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));
 }
All Usage Examples Of CakeRequest::domain