Commit 9af15ac8 by Qiang Xue

Refactored UrlManager.

parent 32ba5122
...@@ -41,8 +41,8 @@ class AccessControl extends ActionFilter ...@@ -41,8 +41,8 @@ class AccessControl extends ActionFilter
); );
/** /**
* @var array a list of access rule objects or configuration arrays for creating the rule objects. * @var array a list of access rule objects or configuration arrays for creating the rule objects.
* If a rule is specified via a configuration array, it will be merged with [[ruleConfig]] before * If a rule is specified via a configuration array, it will be merged with [[ruleConfig]] first
* it is used for creating the rule object. * before it is used for creating the rule object.
* @see ruleConfig * @see ruleConfig
*/ */
public $rules = array(); public $rules = array();
...@@ -55,8 +55,7 @@ class AccessControl extends ActionFilter ...@@ -55,8 +55,7 @@ class AccessControl extends ActionFilter
parent::init(); parent::init();
foreach ($this->rules as $i => $rule) { foreach ($this->rules as $i => $rule) {
if (is_array($rule)) { if (is_array($rule)) {
$rule = array_merge($this->ruleConfig, $rule); $this->rules[$i] = Yii::createObject(array_merge($this->ruleConfig, $rule));
$this->rules[$i] = Yii::createObject($rule);
} }
} }
} }
......
...@@ -29,8 +29,15 @@ class UrlManager extends Component ...@@ -29,8 +29,15 @@ class UrlManager extends Component
/** /**
* @var array the rules for creating and parsing URLs when [[enablePrettyUrl]] is true. * @var array the rules for creating and parsing URLs when [[enablePrettyUrl]] is true.
* This property is used only if [[enablePrettyUrl]] is true. Each element in the array * This property is used only if [[enablePrettyUrl]] is true. Each element in the array
* is the configuration of creating a single URL rule whose class by default is [[defaultRuleClass]]. * is the configuration array for creating a single URL rule. The configuration will
* If you modify this property after the UrlManager object is created, make sure * be merged with [[ruleConfig]] first before it is used for creating the rule object.
*
* A special shortcut format can be used if a rule only specifies [[UrlRule::pattern|pattern]]
* and [[UrlRule::route|route]]: `'pattern' => 'route'`. That is, instead of using a configuration
* array, one can use the key to represent the pattern and the value the corresponding route.
* For example, `'post/<id:\d+>' => 'post/view'`.
*
* Note that if you modify this property after the UrlManager object is created, make sure
* you populate the array with rule objects instead of rule configurations. * you populate the array with rule objects instead of rule configurations.
*/ */
public $rules = array(); public $rules = array();
...@@ -59,15 +66,16 @@ class UrlManager extends Component ...@@ -59,15 +66,16 @@ class UrlManager extends Component
*/ */
public $cache = 'cache'; public $cache = 'cache';
/** /**
* @var string the default class name for creating URL rule instances * @var array the default configuration of URL rules. Individual rule configurations
* when it is not specified in [[rules]]. * specified via [[rules]] will take precedence when the same property of the rule is configured.
*/ */
public $defaultRuleClass = 'yii\web\UrlRule'; public $ruleConfig = array(
'class' => 'yii\web\UrlRule',
);
private $_baseUrl; private $_baseUrl;
private $_hostInfo; private $_hostInfo;
/** /**
* Initializes UrlManager. * Initializes UrlManager.
*/ */
...@@ -101,14 +109,11 @@ class UrlManager extends Component ...@@ -101,14 +109,11 @@ class UrlManager extends Component
foreach ($this->rules as $key => $rule) { foreach ($this->rules as $key => $rule) {
if (!is_array($rule)) { if (!is_array($rule)) {
$rule = array( $rule = array(
'class' => $this->defaultRuleClass,
'pattern' => $key, 'pattern' => $key,
'route' => $rule, 'route' => $rule,
); );
} elseif (!isset($rule['class'])) {
$rule['class'] = $this->defaultRuleClass;
} }
$rules[] = Yii::createObject($rule); $rules[] = Yii::createObject(array_merge($this->ruleConfig, $rule));
} }
$this->rules = $rules; $this->rules = $rules;
......
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