Browse Source

Added UrlRule::host.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
9e948ccd43
  1. 3
      tests/unit/framework/web/UrlManagerTest.php
  2. 6
      tests/unit/framework/web/UrlRuleTest.php
  3. 2
      yii/web/UrlManager.php
  4. 23
      yii/web/UrlRule.php

3
tests/unit/framework/web/UrlManagerTest.php

@ -92,8 +92,9 @@ class UrlManagerTest extends \yiiunit\TestCase
'cache' => null,
'rules' => array(
array(
'pattern' => 'http://<lang:en|fr>.example.com/post/<id>/<title>',
'pattern' => 'post/<id>/<title>',
'route' => 'post/view',
'host' => 'http://<lang:en|fr>.example.com',
),
),
'baseUrl' => '/test',

6
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')),

2
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 {

23
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));

Loading…
Cancel
Save