Commit e25ad4bc by Qiang Xue

URL wip.

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