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.

242 lines
6.8 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
/**
* @var string the base URL of the application (the part after host name and before query string).
* Make sure any ending slashes be removed. If this property is not set, it will be determined
* according to the current request. should be removed. The URLs created via [[createUrl()]] will
* be prefixed with the value of this property.
*/
public $baseUrl;
public $hostInfo;
12 years ago
/**
* Initializes the application component.
*/
public function init()
{
parent::init();
12 years ago
if ($this->enablePrettyUrl && $this->rules !== array()) {
12 years ago
$this->compileRules();
}
}
12 years ago
12 years ago
protected function compileRules()
{
/**
* @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;
12 years ago
}
12 years ago
}
12 years ago
12 years ago
foreach ($this->rules as $i => $rule) {
if (!isset($rule['class'])) {
$rule['class'] = $this->defaultRuleClass;
12 years ago
}
12 years ago
$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
*/
12 years ago
public function parseRequest($request)
12 years ago
{
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, '/');
12 years ago
if ($this->baseUrl === null) {
/** @var $request \yii\web\Request */
$request = Yii::$app->getRequest();
$this->baseUrl = $this->showScriptName || !$this->enablePrettyUrl ? $request->getScriptUrl() : $request->getBaseUrl();
}
12 years ago
12 years ago
if ($this->enablePrettyUrl) {
/** @var $rule UrlRule */
foreach ($this->rules as $rule) {
if (($url = $rule->createUrl($this, $route, $params)) !== false) {
12 years ago
return $this->baseUrl . $url . $anchor;
12 years ago
}
12 years ago
}
12 years ago
if ($this->suffix !== null) {
$route .= $this->suffix;
}
if ($params !== array()) {
$route .= '?' . http_build_query($params);
}
12 years ago
return $this->baseUrl . '/' . $route . $anchor;
12 years ago
} else {
$params[$this->routeVar] = $route;
12 years ago
return $this->baseUrl . '?' . http_build_query($params) . $anchor;
12 years ago
}
12 years ago
}
12 years ago
12 years ago
public function createAbsoluteUrl($route, $params = array(), $hostInfo = null)
{
if ($hostInfo === null) {
$hostInfo = $this->getHostInfo();
}
return $hostInfo . $this->createUrl($route, $params);
}
12 years ago
private $_baseUrl;
12 years ago
/**
* Returns the base URL of the application.
* @return string the base URL of the application (the part after host name and before query string).
* If {@link showScriptName} is true, it will include the script name part.
* Otherwise, it will not, and the ending slashes are stripped off.
*/
12 years ago
public function getBaseUrl()
12 years ago
{
12 years ago
if ($this->_baseUrl === null) {
/** @var $request \yii\web\Request */
$request = Yii::$app->getRequest();
$this->_baseUrl = $this->showScriptName ? $request->getScriptUrl() : $request->getBaseUrl();
}
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
12 years ago
private $_hostInfo;
public function getHostInfo()
{
if ($this->_hostInfo === null) {
/** @var $request \yii\web\Request */
$request = Yii::$app->getRequest();
$this->_hostInfo = $request->getHostInfo();
}
return $this->_baseUrl;
}
public function setHostInfo($value)
{
$this->_hostInfo = $value;
}
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
}