Commit e25ad4bc by Qiang Xue

URL wip.

parent a946a863
<?php <?php
/** /**
* UrlManager class file * UrlRule class file
* *
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008 Yii Software LLC * @copyright Copyright &copy; 2008 Yii Software LLC
...@@ -12,18 +12,7 @@ namespace yii\web; ...@@ -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 ...@@ -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 ...@@ -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 {
......
...@@ -435,6 +435,50 @@ class UrlRuleTest extends \yiiunit\TestCase ...@@ -435,6 +435,50 @@ class UrlRuleTest extends \yiiunit\TestCase
array('post', false), array('post', false),
), ),
), ),
array(
'route has parameters',
array(
'pattern' => '<controller>/<action>',
'route' => '<controller>/<action>',
'defaults' => array(),
),
array(
array('post/index', 'post/index'),
array('module/post/index', false),
),
),
array(
'route has parameters with regex',
array(
'pattern' => '<controller:post|comment>/<action>',
'route' => '<controller>/<action>',
'defaults' => array(),
),
array(
array('post/index', 'post/index'),
array('comment/index', 'comment/index'),
array('test/index', false),
array('post', false),
array('module/post/index', false),
),
),
array(
'route has default parameter',
array(
'pattern' => '<controller:post|comment>/<action>',
'route' => '<controller>/<action>',
'defaults' => array('action' => 'index'),
),
array(
array('post/view', 'post/view'),
array('comment/view', 'comment/view'),
array('test/view', false),
array('post', 'post/index'),
array('posts', false),
array('test', false),
array('index', false),
),
),
); );
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment