You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
5.1 KiB

12 years ago
<?php
/**
* UrlManager class file
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\web;
12 years ago
use Yii;
12 years ago
use \yii\base\Component;
/**
* UrlManager manages the URLs of Yii applications.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class UrlManager extends Component
{
12 years ago
/**
12 years ago
* @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.
12 years ago
*/
public $rules = array();
/**
* @var string the URL suffix used when in 'path' format.
12 years ago
* 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.
12 years ago
*/
12 years ago
public $suffix;
12 years ago
/**
* @var boolean whether to show entry script name in the constructed URL. Defaults to true.
12 years ago
* This property is used only if [[enablePrettyUrl]] is true.
12 years ago
*/
public $showScriptName = true;
/**
* @var string the GET variable name for route. Defaults to 'r'.
*/
12 years ago
public $routeVar = 'r';
12 years ago
/**
12 years ago
* @var string the ID of the cache component that is used to cache the parsed URL rules.
* Defaults to 'cache' which refers to the primary cache component registered with the application.
12 years ago
* Set this property to false if you want to disable caching URL rules.
*/
public $cacheID = 'cache';
/**
12 years ago
* @var string the class name or configuration for URL rule instances.
* This will be passed to [[\Yii::createObject()]] to create the URL rule instances.
12 years ago
*/
12 years ago
public $defaultRuleClass = 'yii\web\UrlRule';
12 years ago
/**
* Initializes the application component.
*/
public function init()
{
parent::init();
12 years ago
if ($this->enablePrettyUrl && $this->rules !== array()) {
/**
* @var $cache \yii\caching\Cache
*/
if ($this->cacheID !== false && ($cache = Yii::$app->getComponent($this->cacheID)) !== null) {
$key = $cache->buildKey(__CLASS__);
$hash = md5(json_encode($this->rules));
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));
12 years ago
}
}
12 years ago
}
/**
* Parses the user request.
12 years ago
* @param Request $request the request component
* @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.
12 years ago
*/
public function parseUrl($request)
{
12 years ago
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());
}
12 years ago
}
public function createUrl($route, $params = array())
{
$anchor = isset($params['#']) ? '#' . $params['#'] : '';
unset($anchor['#']);
12 years ago
$route = trim($route, '/');
if ($this->enablePrettyUrl) {
/** @var $rule UrlRule */
foreach ($this->rules as $rule) {
if (($url = $rule->createUrl($this, $route, $params)) !== false) {
return $this->getBaseUrl() . $url . $anchor;
}
12 years ago
}
12 years ago
if ($this->suffix !== null) {
$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;
12 years ago
}
12 years ago
}
12 years ago
12 years ago
private $_baseUrl;
public function getBaseUrl()
12 years ago
{
12 years ago
return $this->_baseUrl;
}
12 years ago
12 years ago
public function setBaseUrl($value)
{
$this->_baseUrl = trim($value, '/');
12 years ago
}
12 years ago
/**
* Removes the URL suffix from path info.
* @param string $pathInfo path info part in the URL
* @param string $suffix the URL suffix to be removed
* @return string path info with URL suffix removed.
*/
public function removeSuffix($pathInfo, $suffix)
{
$n = strlen($suffix);
if ($n > 0 && substr($pathInfo, -$n) === $suffix) {
return substr($pathInfo, 0, -$n);
} else {
return $pathInfo;
}
}
12 years ago
}