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.

249 lines
7.5 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 handles HTTP request parsing and creation of URLs based on a set of rules.
12 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class UrlManager extends Component
{
12 years ago
/**
* @var boolean whether to enable pretty URLs. Instead of putting all parameters in the query
* string part of a URL, pretty URLs allow using path info to represent some of the parameters
* and can thus produce more user-friendly URLs, such as "/news/Yii-is-released", instead of
* "/index.php?r=news/view&id=100".
12 years ago
*/
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
* you populate the array with rule objects instead of rule 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. This property is used only if [[enablePrettyUrl]] is false.
12 years ago
*/
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.
* Set this property to false if you do not want to cache the URL rules.
12 years ago
*/
public $cacheID = 'cache';
/**
* @var string the default class name for creating URL rule instances
* when it is not specified in [[rules]].
12 years ago
*/
12 years ago
public $defaultRuleClass = 'yii\web\UrlRule';
private $_baseUrl;
private $_hostInfo;
12 years ago
/**
* Initializes the application component.
*/
public function init()
{
parent::init();
$this->compileRules();
12 years ago
}
12 years ago
/**
* Parses the URL rules.
*/
12 years ago
protected function compileRules()
{
if (!$this->enablePrettyUrl || $this->rules === array()) {
return;
}
12 years ago
/**
* @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;
}
} else {
// suffix doesn't match
return false;
12 years ago
}
}
return array($pathInfo, array());
} else {
$route = (string)$request->getParam($this->routeVar);
return array($route, array());
}
12 years ago
}
/**
* Creates a URL using the given route and parameters.
* The URL created is a relative one. Use [[createAbsoluteUrl()]] to create an absolute URL.
* @param string $route the route
* @param array $params the parameters (name-value pairs)
* @return string the created URL
*/
12 years ago
public function createUrl($route, $params = array())
{
$anchor = isset($params['#']) ? '#' . $params['#'] : '';
unset($anchor['#']);
12 years ago
$route = trim($route, '/');
$baseUrl = $this->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) {
return rtrim($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);
}
return rtrim($baseUrl, '/') . '/' . $route . $anchor;
12 years ago
} else {
$url = $baseUrl . '?' . $this->routeVar . '=' . $route;
if ($params !== array()) {
$url .= '&' . http_build_query($params);
}
return $url;
12 years ago
}
12 years ago
}
12 years ago
/**
* Creates an absolute URL using the given route and parameters.
* This method prepends the URL created by [[createUrl()]] with the [[hostInfo]].
* @param string $route the route
* @param array $params the parameters (name-value pairs)
* @return string the created URL
* @see createUrl()
*/
public function createAbsoluteUrl($route, $params = array())
12 years ago
{
return $this->getHostInfo() . $this->createUrl($route, $params);
12 years ago
}
/**
* Returns the base URL that is used by [[createUrl()]] to prepend URLs it creates.
* It defaults to [[Request::scriptUrl]] if [[showScriptName]] is true or [[enablePrettyUrl]] is false;
* otherwise, it defaults to [[Request::baseUrl]].
* @return string the base URL that is used by [[createUrl()]] to prepend URLs it creates.
12 years ago
*/
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 || !$this->enablePrettyUrl ? $request->getScriptUrl() : $request->getBaseUrl();
12 years ago
}
12 years ago
return $this->_baseUrl;
}
12 years ago
/**
* Sets the base URL that is used by [[createUrl()]] to prepend URLs it creates.
* @param string $value the base URL that is used by [[createUrl()]] to prepend URLs it creates.
*/
12 years ago
public function setBaseUrl($value)
{
$this->_baseUrl = $value;
12 years ago
}
12 years ago
/**
* Returns the host info that is used by [[createAbsoluteUrl()]] to prepend URLs it creates.
* @return string the host info (e.g. "http://www.example.com") that is used by [[createAbsoluteUrl()]] to prepend URLs it creates.
*/
12 years ago
public function getHostInfo()
{
if ($this->_hostInfo === null) {
/** @var $request \yii\web\Request */
$request = Yii::$app->getRequest();
$this->_hostInfo = $request->getHostInfo();
}
return $this->_hostInfo;
12 years ago
}
12 years ago
/**
* Sets the host info that is used by [[createAbsoluteUrl()]] to prepend URLs it creates.
* @param string $value the host info (e.g. "http://www.example.com") that is used by [[createAbsoluteUrl()]] to prepend URLs it creates.
12 years ago
*/
public function setHostInfo($value)
12 years ago
{
$this->_hostInfo = rtrim($value, '/');
12 years ago
}
12 years ago
}