Created
July 30, 2013 08:42
-
-
Save tigrang/6111301 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SubdomainRoute extends CakeRoute { | |
/** | |
* @var CakeRequest | |
*/ | |
private $Request; | |
/** | |
* Length of domain's TLD | |
* | |
* @var int | |
*/ | |
public static $tldLength = 1; | |
/** | |
* Default options | |
* | |
* @var array | |
*/ | |
public $options = array(); | |
/** | |
* Constructor for a Route | |
* | |
* @param string $template Template string with parameter placeholders | |
* @param array $defaults Array of defaults for the route. | |
* @param array $options Array of additional options for the Route | |
*/ | |
public function __construct($template, $defaults = array(), $options = array()) { | |
parent::__construct($template, $defaults, $options); | |
$this->Request = new CakeRequest(); | |
$this->options = array_merge(array('protocol' => 'http', 'subdomain' => 'www'), $options); | |
} | |
public function match($url) { | |
$url = parent::match($url); | |
return $url; | |
} | |
protected function _writeUrl($params) { | |
$protocol = $this->options['protocol']; | |
if (isset($url['protocol'])) { | |
$protocol = $url['protocol']; | |
unset($url['protocol']); | |
} | |
$subdomain = $this->options['subdomain']; | |
if (isset($url['subdomain'])) { | |
$subdomain = $url['subdomain']; | |
unset($url['subdomain']); | |
} | |
$url = parent::_writeUrl($params); | |
$domain = $this->_getDomain(); | |
return "{$protocol}://{$subdomain}.{$domain}{$url}"; | |
} | |
public function parse($url) { | |
$url = parent::parse($url); | |
if (!in_array($this->options['subdomain'], $this->_getSubdomains())) { | |
return false; | |
} | |
return $url; | |
} | |
/** | |
* Gets list of subdomains from current URL | |
* | |
* @return array Of subdomains | |
*/ | |
protected function _getSubdomains() { | |
return $this->Request->subdomains(self::$tldLength); | |
} | |
/** | |
* Get domain name | |
* | |
* @return string | |
*/ | |
protected function _getDomain() { | |
return $this->Request->domain(self::$tldLength); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment