From 9e948ccd432a7b23255d488c3d9bf9759b1caa83 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Thu, 16 May 2013 23:54:06 -0400 Subject: [PATCH] Added UrlRule::host. --- tests/unit/framework/web/UrlManagerTest.php | 3 ++- tests/unit/framework/web/UrlRuleTest.php | 6 ++++-- yii/web/UrlManager.php | 2 +- yii/web/UrlRule.php | 23 ++++++++++++----------- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/tests/unit/framework/web/UrlManagerTest.php b/tests/unit/framework/web/UrlManagerTest.php index ed2a70e..b6246c6 100644 --- a/tests/unit/framework/web/UrlManagerTest.php +++ b/tests/unit/framework/web/UrlManagerTest.php @@ -92,8 +92,9 @@ class UrlManagerTest extends \yiiunit\TestCase 'cache' => null, 'rules' => array( array( - 'pattern' => 'http://.example.com/post//', + 'pattern' => 'post/<id>/<title>', 'route' => 'post/view', + 'host' => 'http://<lang:en|fr>.example.com', ), ), 'baseUrl' => '/test', diff --git a/tests/unit/framework/web/UrlRuleTest.php b/tests/unit/framework/web/UrlRuleTest.php index 85d410a..e0761ba 100644 --- a/tests/unit/framework/web/UrlRuleTest.php +++ b/tests/unit/framework/web/UrlRuleTest.php @@ -330,9 +330,10 @@ class UrlRuleTest extends \yiiunit\TestCase array( 'with host info', array( - 'pattern' => 'http://<lang:(en|fr)>.example.com/post/<page:\d+>/<tag>', + 'pattern' => 'post/<page:\d+>/<tag>', 'route' => 'post/index', 'defaults' => array('page' => 1), + 'host' => 'http://<lang:en|fr>.example.com', ), array( array('post/index', array('page' => 1, 'tag' => 'a'), false), @@ -625,8 +626,9 @@ class UrlRuleTest extends \yiiunit\TestCase array( 'with host info', array( - 'pattern' => 'http://<lang:en|fr>.example.com/post/<page:\d+>', + 'pattern' => 'post/<page:\d+>', 'route' => 'post/index', + 'host' => 'http://<lang:en|fr>.example.com', ), array( array('post/1', 'post/index', array('page' => '1', 'lang' => 'en')), diff --git a/yii/web/UrlManager.php b/yii/web/UrlManager.php index 6392d11..dde10ee 100644 --- a/yii/web/UrlManager.php +++ b/yii/web/UrlManager.php @@ -183,7 +183,7 @@ class UrlManager extends Component /** @var $rule UrlRule */ foreach ($this->rules as $rule) { if (($url = $rule->createUrl($this, $route, $params)) !== false) { - if ($rule->hasHostInfo) { + if ($rule->host !== null) { if ($baseUrl !== '' && ($pos = strpos($url, '/', 8)) !== false) { return substr($url, 0, $pos) . $baseUrl . substr($url, $pos); } else { diff --git a/yii/web/UrlRule.php b/yii/web/UrlRule.php index 7243b1b..b1e74da 100644 --- a/yii/web/UrlRule.php +++ b/yii/web/UrlRule.php @@ -28,10 +28,16 @@ class UrlRule extends Object const CREATION_ONLY = 2; /** - * @var string the pattern used to parse and create URLs. + * @var string the pattern used to parse and create the path info part of a URL. + * @see host */ public $pattern; /** + * @var string the pattern used to parse and create the host info part of a URL. + * @see pattern + */ + public $host; + /** * @var string the route to the controller action */ public $route; @@ -62,11 +68,6 @@ class UrlRule extends Object * If it is [[CREATION_ONLY]], the rule is for URL creation only. */ public $mode; - /** - * @var boolean whether this URL rule contains the host info part. - * This property is set after the URL rule is parsed. - */ - public $hasHostInfo; /** * @var string the template for generating a new URL. This is derived from [[pattern]] and is used in generating URL. @@ -108,9 +109,9 @@ class UrlRule extends Object $this->pattern = trim($this->pattern, '/'); - $this->hasHostInfo = !strncasecmp($this->pattern, 'http://', 7) || !strncasecmp($this->pattern, 'https://', 8); - - if ($this->pattern === '') { + if ($this->host !== null) { + $this->pattern = rtrim($this->host, '/') . rtrim('/' . $this->pattern, '/') . '/'; + } elseif ($this->pattern === '') { $this->_template = ''; $this->pattern = '#^$#u'; return; @@ -190,7 +191,7 @@ class UrlRule extends Object } } - if ($this->hasHostInfo) { + if ($this->host !== null) { $pathInfo = strtolower($request->getHostInfo()) . '/' . $pathInfo; } @@ -279,7 +280,7 @@ class UrlRule extends Object } $url = trim(strtr($this->_template, $tr), '/'); - if ($this->hasHostInfo) { + if ($this->host !== null) { $pos = strpos($url, '/', 8); if ($pos !== false) { $url = substr($url, 0, $pos) . preg_replace('#/+#', '/', substr($url, $pos));