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, 'cache' => null,
'rules' => array( 'rules' => array(
array( array(
'pattern' => 'http://<lang:en|fr>.example.com/post/<id>/<title>', 'pattern' => 'post/<id>/<title>',
'route' => 'post/view', 'route' => 'post/view',
'host' => 'http://<lang:en|fr>.example.com',
), ),
), ),
'baseUrl' => '/test', 'baseUrl' => '/test',

6
tests/unit/framework/web/UrlRuleTest.php

@ -330,9 +330,10 @@ class UrlRuleTest extends \yiiunit\TestCase
array( array(
'with host info', 'with host info',
array( array(
'pattern' => 'http://<lang:(en|fr)>.example.com/post/<page:\d+>/<tag>', 'pattern' => 'post/<page:\d+>/<tag>',
'route' => 'post/index', 'route' => 'post/index',
'defaults' => array('page' => 1), 'defaults' => array('page' => 1),
'host' => 'http://<lang:en|fr>.example.com',
), ),
array( array(
array('post/index', array('page' => 1, 'tag' => 'a'), false), array('post/index', array('page' => 1, 'tag' => 'a'), false),
@ -625,8 +626,9 @@ class UrlRuleTest extends \yiiunit\TestCase
array( array(
'with host info', 'with host info',
array( array(
'pattern' => 'http://<lang:en|fr>.example.com/post/<page:\d+>', 'pattern' => 'post/<page:\d+>',
'route' => 'post/index', 'route' => 'post/index',
'host' => 'http://<lang:en|fr>.example.com',
), ),
array( array(
array('post/1', 'post/index', array('page' => '1', 'lang' => 'en')), 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 */ /** @var $rule UrlRule */
foreach ($this->rules as $rule) { foreach ($this->rules as $rule) {
if (($url = $rule->createUrl($this, $route, $params)) !== false) { if (($url = $rule->createUrl($this, $route, $params)) !== false) {
if ($rule->hasHostInfo) { if ($rule->host !== null) {
if ($baseUrl !== '' && ($pos = strpos($url, '/', 8)) !== false) { if ($baseUrl !== '' && ($pos = strpos($url, '/', 8)) !== false) {
return substr($url, 0, $pos) . $baseUrl . substr($url, $pos); return substr($url, 0, $pos) . $baseUrl . substr($url, $pos);
} else { } else {

23
yii/web/UrlRule.php

@ -28,10 +28,16 @@ class UrlRule extends Object
const CREATION_ONLY = 2; 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; 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 * @var string the route to the controller action
*/ */
public $route; public $route;
@ -62,11 +68,6 @@ class UrlRule extends Object
* If it is [[CREATION_ONLY]], the rule is for URL creation only. * If it is [[CREATION_ONLY]], the rule is for URL creation only.
*/ */
public $mode; 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. * @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->pattern = trim($this->pattern, '/');
$this->hasHostInfo = !strncasecmp($this->pattern, 'http://', 7) || !strncasecmp($this->pattern, 'https://', 8); if ($this->host !== null) {
$this->pattern = rtrim($this->host, '/') . rtrim('/' . $this->pattern, '/') . '/';
if ($this->pattern === '') { } elseif ($this->pattern === '') {
$this->_template = ''; $this->_template = '';
$this->pattern = '#^$#u'; $this->pattern = '#^$#u';
return; return;
@ -190,7 +191,7 @@ class UrlRule extends Object
} }
} }
if ($this->hasHostInfo) { if ($this->host !== null) {
$pathInfo = strtolower($request->getHostInfo()) . '/' . $pathInfo; $pathInfo = strtolower($request->getHostInfo()) . '/' . $pathInfo;
} }
@ -279,7 +280,7 @@ class UrlRule extends Object
} }
$url = trim(strtr($this->_template, $tr), '/'); $url = trim(strtr($this->_template, $tr), '/');
if ($this->hasHostInfo) { if ($this->host !== null) {
$pos = strpos($url, '/', 8); $pos = strpos($url, '/', 8);
if ($pos !== false) { if ($pos !== false) {
$url = substr($url, 0, $pos) . preg_replace('#/+#', '/', substr($url, $pos)); $url = substr($url, 0, $pos) . preg_replace('#/+#', '/', substr($url, $pos));

Loading…
Cancel
Save