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.

164 lines
5.0 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;
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
/**
* @var array the URL rules (pattern=>route).
*/
public $rules = array();
/**
* @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.
*/
12 years ago
public $suffix;
12 years ago
/**
* @var boolean whether to show entry script name in the constructed URL. Defaults to 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'.
*/
12 years ago
public $routeVar = 'r';
12 years ago
/**
* @var boolean whether routes are case-sensitive. Defaults to true. By setting this to false,
* the route in the incoming request will be turned to lower case first before further processing.
* 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.
*/
public $cacheID = 'cache';
/**
* @var boolean whether to enable strict URL parsing.
* This property is only effective when {@link urlFormat} is 'path'.
* 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;
/**
* @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';
12 years ago
/**
* Initializes the application component.
*/
public function init()
{
parent::init();
$this->processRules();
}
/**
* Processes the URL rules.
*/
protected function processRules()
{
12 years ago
foreach ($this->rules as $i => $rule) {
if (!isset($rule['class'])) {
$rule['class'] = 'yii\web\UrlRule';
}
$this->rules[$i] = \Yii::createObject($rule);
}
12 years ago
}
/**
* Parses the user request.
12 years ago
* @param Request $request the request application component
12 years ago
* @return string the route (controllerID/actionID) and perhaps GET parameters in path format.
*/
public function parseUrl($request)
{
12 years ago
}
public function createUrl($route, $params = array())
{
$anchor = isset($params['#']) ? '#' . $params['#'] : '';
unset($anchor['#']);
/** @var $rule UrlRule */
foreach ($this->rules as $rule) {
12 years ago
if (($url = $rule->createUrl($this, $route, $params)) !== false) {
12 years ago
return $this->getBaseUrl() . $url . $anchor;
}
12 years ago
}
12 years ago
if ($params !== array()) {
$route .= '?' . http_build_query($params);
}
return $this->getBaseUrl() . '/' . $route . $anchor;
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
}