Browse Source

url wip

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
c08c2e8b72
  1. 2
      framework/caching/Cache.php
  2. 151
      framework/web/UrlManager.php
  3. 5
      framework/web/UrlRule.php

2
framework/caching/Cache.php

@ -95,7 +95,7 @@ abstract class Cache extends Component implements \ArrayAccess
return (string)$key; return (string)$key;
} else { } else {
$params = func_get_args(); $params = func_get_args();
return md5(serialize($params)); return md5(json_encode($params));
} }
} }

151
framework/web/UrlManager.php

@ -9,6 +9,7 @@
namespace yii\web; namespace yii\web;
use Yii;
use \yii\base\Component; use \yii\base\Component;
/** /**
@ -20,69 +21,43 @@ use \yii\base\Component;
class UrlManager extends Component class UrlManager extends Component
{ {
/** /**
* @var array the URL rules (pattern=>route). * @var boolean whether to enable pretty URLs.
*/
public $enablePrettyUrl = false;
/**
* @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
* is the configuration of creating a single URL rule whose class by default is [[defaultRuleClass]].
* If you modify this property after the UrlManager object is created, make sure
* populate the array with the rule objects instead of configurations.
*/ */
public $rules = array(); public $rules = array();
/** /**
* @var string the URL suffix used when in 'path' format. * @var string the URL suffix used when in 'path' format.
* For example, ".html" can be used so that the URL looks like pointing to a static HTML page. Defaults to empty. * For example, ".html" can be used so that the URL looks like pointing to a static HTML page.
* This property is used only if [[enablePrettyUrl]] is true.
*/ */
public $suffix; public $suffix;
/** /**
* @var boolean whether to show entry script name in the constructed URL. Defaults to true. * @var boolean whether to show entry script name in the constructed URL. Defaults to true.
* This property is used only if [[enablePrettyUrl]] is true.
*/ */
public $showScriptName = true; public $showScriptName = true;
/** /**
* @var boolean whether to append GET parameters to the path info part. Defaults to true.
* This property is only effective when {@link urlFormat} is 'path' and is mainly used when
* creating URLs. When it is true, GET parameters will be appended to the path info and
* separate from each other using slashes. If this is false, GET parameters will be in query part.
*/
public $appendParams = true;
/**
* @var string the GET variable name for route. Defaults to 'r'. * @var string the GET variable name for route. Defaults to 'r'.
*/ */
public $routeVar = 'r'; public $routeVar = 'r';
/** /**
* @var boolean whether routes are case-sensitive. Defaults to true. By setting this to false, * @var string the ID of the cache component that is used to cache the parsed URL rules.
* the route in the incoming request will be turned to lower case first before further processing. * Defaults to 'cache' which refers to the primary cache component registered with the application.
* As a result, you should follow the convention that you use lower case when specifying
* controller mapping ({@link CWebApplication::controllerMap}) and action mapping
* ({@link CController::actions}). Also, the directory names for organizing controllers should
* be in lower case.
*/
public $caseSensitive = true;
/**
* @var boolean whether the GET parameter values should match the corresponding
* sub-patterns in a rule before using it to create a URL. Defaults to false, meaning
* a rule will be used for creating a URL only if its route and parameter names match the given ones.
* If this property is set true, then the given parameter values must also match the corresponding
* parameter sub-patterns. Note that setting this property to true will degrade performance.
* @since 1.1.0
*/
public $matchValue = false;
/**
* @var string the ID of the cache application component that is used to cache the parsed URL rules.
* Defaults to 'cache' which refers to the primary cache application component.
* Set this property to false if you want to disable caching URL rules. * Set this property to false if you want to disable caching URL rules.
*/ */
public $cacheID = 'cache'; public $cacheID = 'cache';
/** /**
* @var boolean whether to enable strict URL parsing. * @var string the class name or configuration for URL rule instances.
* This property is only effective when {@link urlFormat} is 'path'. * This will be passed to [[\Yii::createObject()]] to create the URL rule instances.
* If it is set true, then an incoming URL must match one of the {@link rules URL rules}.
* Otherwise, it will be treated as an invalid request and trigger a 404 HTTP exception.
* Defaults to false.
*/ */
public $useStrictParsing = false; public $defaultRuleClass = 'yii\web\UrlRule';
/**
* @var string the class name or path alias for the URL rule instances. Defaults to 'CUrlRule'.
* If you change this to something else, please make sure that the new class must extend from
* {@link CBaseUrlRule} and have the same constructor signature as {@link CUrlRule}.
* It must also be serializable and autoloadable.
* @since 1.1.8
*/
public $urlRuleClass = 'CUrlRule';
/** /**
* Initializes the application component. * Initializes the application component.
@ -90,29 +65,67 @@ class UrlManager extends Component
public function init() public function init()
{ {
parent::init(); parent::init();
$this->processRules();
}
/** if ($this->enablePrettyUrl && $this->rules !== array()) {
* Processes the URL rules. /**
*/ * @var $cache \yii\caching\Cache
protected function processRules() */
{ if ($this->cacheID !== false && ($cache = Yii::$app->getComponent($this->cacheID)) !== null) {
foreach ($this->rules as $i => $rule) { $key = $cache->buildKey(__CLASS__);
if (!isset($rule['class'])) { $hash = md5(json_encode($this->rules));
$rule['class'] = 'yii\web\UrlRule'; if (($data = $cache->get($key)) !== false && isset($data[1]) && $data[1] === $hash) {
$this->rules = $data[0];
return;
}
}
foreach ($this->rules as $i => $rule) {
if (!isset($rule['class'])) {
$rule['class'] = $this->defaultRuleClass;
}
$this->rules[$i] = Yii::createObject($rule);
}
if (isset($cache)) {
$cache->set($key, array($this->rules, $hash));
} }
$this->rules[$i] = \Yii::createObject($rule);
} }
} }
/** /**
* Parses the user request. * Parses the user request.
* @param Request $request the request application component * @param Request $request the request component
* @return string the route (controllerID/actionID) and perhaps GET parameters in path format. * @return array|boolean the route and the associated parameters. The latter is always empty
* if [[enablePrettyUrl]] is false. False is returned if the current request cannot be successfully parsed.
*/ */
public function parseUrl($request) public function parseUrl($request)
{ {
if ($this->enablePrettyUrl) {
$pathInfo = $request->pathInfo;
/** @var $rule UrlRule */
foreach ($this->rules as $rule) {
if (($result = $rule->parseUrl($this, $pathInfo)) !== false) {
return $result;
}
}
$suffix = (string)$this->suffix;
if ($suffix !== '' && $suffix !== '/' && $pathInfo !== '') {
$n = strlen($this->suffix);
if (substr($pathInfo, -$n) === $this->suffix) {
$pathInfo = substr($pathInfo, 0, -$n);
if ($pathInfo === '') {
// suffix alone is not allowed
return false;
}
}
}
return array($pathInfo, array());
} else {
$route = (string)$request->getParam($this->routeVar);
return array($route, array());
}
} }
public function createUrl($route, $params = array()) public function createUrl($route, $params = array())
@ -120,17 +133,27 @@ class UrlManager extends Component
$anchor = isset($params['#']) ? '#' . $params['#'] : ''; $anchor = isset($params['#']) ? '#' . $params['#'] : '';
unset($anchor['#']); unset($anchor['#']);
/** @var $rule UrlRule */ $route = trim($route, '/');
foreach ($this->rules as $rule) {
if (($url = $rule->createUrl($this, $route, $params)) !== false) { if ($this->enablePrettyUrl) {
return $this->getBaseUrl() . $url . $anchor; /** @var $rule UrlRule */
foreach ($this->rules as $rule) {
if (($url = $rule->createUrl($this, $route, $params)) !== false) {
return $this->getBaseUrl() . $url . $anchor;
}
} }
}
if ($params !== array()) { if ($this->suffix !== null) {
$route .= '?' . http_build_query($params); $route .= $this->suffix;
}
if ($params !== array()) {
$route .= '?' . http_build_query($params);
}
return $this->getBaseUrl() . '/' . $route . $anchor;
} else {
$params[$this->routeVar] = $route;
return $this->getBaseUrl() . '?' . http_build_query($params) . $anchor;
} }
return $this->getBaseUrl() . '/' . $route . $anchor;
} }
private $_baseUrl; private $_baseUrl;

5
framework/web/UrlRule.php

@ -56,11 +56,6 @@ class UrlRule extends Object
*/ */
public $verb; public $verb;
/** /**
* @var string the host info (e.g. `http://www.example.com`) that this rule should match.
* If not set, it means the host info is ignored.
*/
public $hostInfo;
/**
* @var integer a value indicating if this rule should be used for both URL parsing and creation, * @var integer a value indicating if this rule should be used for both URL parsing and creation,
* parsing only, or creation only. * parsing only, or creation only.
* If not set, it means the rule is both URL parsing and creation. * If not set, it means the rule is both URL parsing and creation.

Loading…
Cancel
Save