|
|
@ -1,6 +1,6 @@ |
|
|
|
<?php |
|
|
|
<?php |
|
|
|
/** |
|
|
|
/** |
|
|
|
* UrlManager class file |
|
|
|
* UrlRule class file |
|
|
|
* |
|
|
|
* |
|
|
|
* @link http://www.yiiframework.com/ |
|
|
|
* @link http://www.yiiframework.com/ |
|
|
|
* @copyright Copyright © 2008 Yii Software LLC |
|
|
|
* @copyright Copyright © 2008 Yii Software LLC |
|
|
@ -12,18 +12,7 @@ namespace yii\web; |
|
|
|
use yii\base\Object; |
|
|
|
use yii\base\Object; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* UrlManager manages the URLs of Yii applications. |
|
|
|
* UrlRule represents a rule used for parsing and generating URLs. |
|
|
|
* array( |
|
|
|
|
|
|
|
* 'pattern' => 'post/<page:\d+>', |
|
|
|
|
|
|
|
* 'route' => 'post/view', |
|
|
|
|
|
|
|
* 'defaults' => array('page' => 1), |
|
|
|
|
|
|
|
* ) |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* array( |
|
|
|
|
|
|
|
* 'pattern' => 'about', |
|
|
|
|
|
|
|
* 'route' => 'site/page', |
|
|
|
|
|
|
|
* 'defaults' => array('view' => 'about'), |
|
|
|
|
|
|
|
* ) |
|
|
|
|
|
|
|
* |
|
|
|
* |
|
|
|
* @author Qiang Xue <qiang.xue@gmail.com> |
|
|
|
* @author Qiang Xue <qiang.xue@gmail.com> |
|
|
|
* @since 2.0 |
|
|
|
* @since 2.0 |
|
|
@ -45,13 +34,36 @@ class UrlRule extends Object |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $defaults = array(); |
|
|
|
public $defaults = array(); |
|
|
|
|
|
|
|
|
|
|
|
protected $paramRules = array(); |
|
|
|
/** |
|
|
|
protected $routeRule; |
|
|
|
* @var string the template for generating a new URL. This is derived from [[pattern]] and is used in generating URL. |
|
|
|
|
|
|
|
*/ |
|
|
|
protected $template; |
|
|
|
protected $template; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @var string the regex for matching the route part. This is used in generating URL. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
protected $routeRule; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @var array list of regex for matching parameters. This is used in generating URL. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
protected $paramRules = array(); |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @var array list of parameters used in the route. |
|
|
|
|
|
|
|
*/ |
|
|
|
protected $routeParams = array(); |
|
|
|
protected $routeParams = array(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Initializes this rule. |
|
|
|
|
|
|
|
*/ |
|
|
|
public function init() |
|
|
|
public function init() |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
$this->compileRule(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Compiles the rule using the current configuration. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
protected function compileRule() |
|
|
|
|
|
|
|
{ |
|
|
|
$this->pattern = trim($this->pattern, '/'); |
|
|
|
$this->pattern = trim($this->pattern, '/'); |
|
|
|
if ($this->pattern === '') { |
|
|
|
if ($this->pattern === '') { |
|
|
|
$this->template = ''; |
|
|
|
$this->template = ''; |
|
|
@ -105,17 +117,21 @@ class UrlRule extends Object |
|
|
|
if (!preg_match($this->pattern, $pathInfo, $matches)) { |
|
|
|
if (!preg_match($this->pattern, $pathInfo, $matches)) { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
foreach ($this->defaults as $name => $value) { |
|
|
|
|
|
|
|
if (!isset($matches[$name]) || $matches[$name] === '') { |
|
|
|
|
|
|
|
$matches[$name] = $value; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
$params = $this->defaults; |
|
|
|
$params = $this->defaults; |
|
|
|
$tr = array(); |
|
|
|
$tr = array(); |
|
|
|
foreach ($matches as $name => $value) { |
|
|
|
foreach ($matches as $name => $value) { |
|
|
|
if ($value !== '') { |
|
|
|
|
|
|
|
if (isset($this->routeParams[$name])) { |
|
|
|
if (isset($this->routeParams[$name])) { |
|
|
|
$tr[$this->routeParams[$name]] = $value; |
|
|
|
$tr[$this->routeParams[$name]] = $value; |
|
|
|
|
|
|
|
unset($params[$name]); |
|
|
|
} elseif (isset($this->paramRules[$name])) { |
|
|
|
} elseif (isset($this->paramRules[$name])) { |
|
|
|
$params[$name] = $value; |
|
|
|
$params[$name] = $value; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if ($this->routeRule !== null) { |
|
|
|
if ($this->routeRule !== null) { |
|
|
|
$route = strtr($this->route, $tr); |
|
|
|
$route = strtr($this->route, $tr); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|