CUrlRule

优质
小牛编辑
138浏览
2023-12-01
所有包 | 属性 | 方法
system.web
继承class CUrlRule » CBaseUrlRule » CComponent
源自1.0
版本$Id: CUrlManager.php 3515 2011-12-28 12:29:24Z mdomba $
源码framework/web/CUrlManager.php
CUrlRule represents a URL formatting/parsing rule.

It mainly consists of two parts: route and pattern. The former classifies the rule so that it only applies to specific controller-action route. The latter performs the actual formatting and parsing role. The pattern may have a set of named parameters.

公共属性

隐藏继承属性

属性类型描述定义在
appendbooleanwhether the URL allows additional parameters at the end of the path info.CUrlRule
caseSensitivebooleanwhether the rule is case sensitive.CUrlRule
defaultParamsarraythe default GET parameters (name=>value) that this rule provides.CUrlRule
hasHostInfobooleanwhether host info should be considered for this ruleCUrlRule
matchValuebooleanwhether the GET parameter values should match the corresponding sub-patterns in the rule when creating a URL.CUrlRule
paramsarraylist of parameters (name=>regular expression)CUrlRule
parsingOnlybooleanwhether this rule is only used for request parsing.CUrlRule
patternstringregular expression used to parse a URLCUrlRule
referencesarraythe mapping from route param name to token name (e.CUrlRule
routestringthe controller/action pairCUrlRule
routePatternstringthe pattern used to match routeCUrlRule
templatestringtemplate used to construct a URLCUrlRule
urlSuffixstringthe URL suffix used for this rule.CUrlRule
verbstringthe HTTP verb (e.CUrlRule

公共方法

隐藏继承方法

方法描述定义在
__call()如果类中没有调的方法名,则调用这个方法。CComponent
__construct()Constructor.CUrlRule
__get()返回一个属性值、一个事件处理程序列表或一个行为名称。CComponent
__isset()检查一个属性是否为null。CComponent
__set()设置一个组件的属性值。CComponent
__unset()设置一个组件的属性为null。CComponent
asa()返回这个名字的行为对象。CComponent
attachBehavior()附加一个行为到组件。CComponent
attachBehaviors()附加一个行为列表到组件。CComponent
attachEventHandler()为事件附加一个事件处理程序。CComponent
canGetProperty()确定属性是否可读。CComponent
canSetProperty()确定属性是否可写。CComponent
createUrl()Creates a URL based on this rule.CUrlRule
detachBehavior()从组件中分离一个行为。CComponent
detachBehaviors()从组件中分离所有行为。CComponent
detachEventHandler()分离一个存在的事件处理程序。CComponent
disableBehavior()禁用一个附加行为。CComponent
disableBehaviors()禁用组件附加的所有行为。CComponent
enableBehavior()启用一个附加行为。CComponent
enableBehaviors()启用组件附加的所有行为。CComponent
evaluateExpression()计算一个PHP表达式,或根据组件上下文执行回调。CComponent
getEventHandlers()返回一个事件的附加处理程序列表。CComponent
hasEvent()确定一个事件是否定义。CComponent
hasEventHandler()检查事件是否有附加的处理程序。CComponent
hasProperty()确定属性是否被定义。CComponent
parseUrl()Parses a URL based on this rule.CUrlRule
raiseEvent()发起一个事件。CComponent

属性详细

append 属性 public boolean $append;

whether the URL allows additional parameters at the end of the path info.

caseSensitive 属性 public boolean $caseSensitive;

whether the rule is case sensitive. Defaults to null, meaning using the value of CUrlManager::caseSensitive.

defaultParams 属性 public array $defaultParams;

the default GET parameters (name=>value) that this rule provides. When this rule is used to parse the incoming request, the values declared in this property will be injected into $_GET.

hasHostInfo 属性 public boolean $hasHostInfo;

whether host info should be considered for this rule

matchValue 属性 (可用自 v1.1.0) public boolean $matchValue;

whether the GET parameter values should match the corresponding sub-patterns in the rule when creating a URL. Defaults to null, meaning using the value of CUrlManager::matchValue. When this property is false, it means a rule will be used for creating a URL 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.

params 属性 public array $params;

list of parameters (name=>regular expression)

parsingOnly 属性 (可用自 v1.1.7) public boolean $parsingOnly;

whether this rule is only used for request parsing. Defaults to false, meaning the rule is used for both URL parsing and creation.

pattern 属性 public string $pattern;

regular expression used to parse a URL

references 属性 public array $references;

the mapping from route param name to token name (e.g. _r1=>)

route 属性 public string $route;

the controller/action pair

routePattern 属性 public string $routePattern;

the pattern used to match route

template 属性 public string $template;

template used to construct a URL

urlSuffix 属性 public string $urlSuffix;

the URL suffix used for this rule. For example, ".html" can be used so that the URL looks like pointing to a static HTML page. Defaults to null, meaning using the value of CUrlManager::urlSuffix.

verb 属性 (可用自 v1.1.7) public string $verb;

the HTTP verb (e.g. GET, POST, DELETE) that this rule should match. If this rule can match multiple verbs, please separate them with commas. If this property is not set, the rule can match any verb. Note that this property is only used when parsing a request. It is ignored for URL creation.

方法详细

__construct() 方法
public void __construct(string $route, string $pattern)
$routestringthe route of the URL (controller/action)
$patternstringthe pattern for matching the URL
源码: framework/web/CUrlManager.php#644 (显示) publicfunction__construct($route,$pattern)
{
if(is_array($route))
{
foreach(array('urlSuffix','caseSensitive','defaultParams','matchValue','verb','parsingOnly')as$name)
{
if(isset($route[$name]))
$this->$name=$route[$name];
}
if(isset($route['pattern']))
$pattern=$route['pattern'];
$route=$route[0];
}
$this->route=trim($route,'/');

$tr2['/']=$tr['/']='\/';

if(strpos($route,'<')!==false&&preg_match_all('/<(w+)>/',$route,$matches2))
{
foreach($matches2[1]as$name)
$this->references[$name]="<$name>";
}

$this->hasHostInfo=!strncasecmp($pattern,'http://',7)||!strncasecmp($pattern,'https://',8);

if($this->verb!==null)
$this->verb=preg_split('/[s,]+/',strtoupper($this->verb),-1,PREG_SPLIT_NO_EMPTY);

if(preg_match_all('/<(w+):?(.*?)?>/',$pattern,$matches))
{
$tokens=array_combine($matches[1],$matches[2]);
foreach($tokensas$name=>$value)
{
if($value==='')
$value='[^/]+';
$tr["<$name>"]="(?P<$name>$value)";
if(isset($this->references[$name]))
$tr2["<$name>"]=$tr["<$name>"];
else
$this->params[$name]=$value;
}
}
$p=rtrim($pattern,'*');
$this->append=$p!==$pattern;
$p=trim($p,'/');
$this->template=preg_replace('/<(w+):?.*?>/','<$1>',$p);
$this->pattern='/^'.strtr($this->template,$tr).'/';
if($this->append)
$this->pattern.='/u';
else
$this->pattern.='$/u';

if($this->references!==array())
$this->routePattern='/^'.strtr($this->route,$tr2).'$/u';

if(YII_DEBUG&&@preg_match($this->pattern,'test')===false)
thrownewCException(Yii::t('yii','TheURLpattern"{pattern}"forroute"{route}"isnotavalidregularexpression.',
array('{route}'=>$route,'{pattern}'=>$pattern)));
}

Constructor.

createUrl() 方法
public mixed createUrl(CUrlManager $manager, string $route, array $params, string $ampersand)
$managerCUrlManagerthe manager
$routestringthe route
$paramsarraylist of parameters
$ampersandstringthe token separating name-value pairs in the URL.
{return}mixedthe constructed URL or false on error
源码: framework/web/CUrlManager.php#712 (显示) publicfunctioncreateUrl($manager,$route,$params,$ampersand)
{
if($this->parsingOnly)
returnfalse;

if($manager->caseSensitive&&$this->caseSensitive===null||$this->caseSensitive)
$case='';
else
$case='i';

$tr=array();
if($route!==$this->route)
{
if($this->routePattern!==null&&preg_match($this->routePattern.$case,$route,$matches))
{
foreach($this->referencesas$key=>$name)
$tr[$name]=$matches[$key];
}
else
returnfalse;
}

foreach($this->defaultParamsas$key=>$value)
{
if(isset($params[$key]))
{
if($params[$key]==$value)
unset($params[$key]);
else
returnfalse;
}
}

foreach($this->paramsas$key=>$value)
if(!isset($params[$key]))
returnfalse;

if($manager->matchValue&&$this->matchValue===null||$this->matchValue)
{
foreach($this->paramsas$key=>$value)
{
if(!preg_match('/'.$value.'/'.$case,$params[$key]))
returnfalse;
}
}

foreach($this->paramsas$key=>$value)
{
$tr["<$key>"]=urlencode($params[$key]);
unset($params[$key]);
}

$suffix=$this->urlSuffix===null?$manager->urlSuffix:$this->urlSuffix;

$url=strtr($this->template,$tr);

if($this->hasHostInfo)
{
$hostInfo=Yii::app()->getRequest()->getHostInfo();
if(stripos($url,$hostInfo)===0)
$url=substr($url,strlen($hostInfo));
}

if(empty($params))
return$url!==''?$url.$suffix:$url;

if($this->append)
$url.='/'.$manager->createPathInfo($params,'/','/').$suffix;
else
{
if($url!=='')
$url.=$suffix;
$url.='?'.$manager->createPathInfo($params,'=',$ampersand);
}

return$url;
}

Creates a URL based on this rule.

parseUrl() 方法
public mixed parseUrl(CUrlManager $manager, CHttpRequest $request, string $pathInfo, string $rawPathInfo)
$managerCUrlManagerthe URL manager
$requestCHttpRequestthe request object
$pathInfostringpath info part of the URL
$rawPathInfostringpath info that contains the potential URL suffix
{return}mixedthe route that consists of the controller ID and action ID or false on error
源码: framework/web/CUrlManager.php#798 (显示) publicfunctionparseUrl($manager,$request,$pathInfo,$rawPathInfo)
{
if($this->verb!==null&&!in_array($request->getRequestType(),$this->verb,true))
returnfalse;

if($manager->caseSensitive&&$this->caseSensitive===null||$this->caseSensitive)
$case='';
else
$case='i';

if($this->urlSuffix!==null)
$pathInfo=$manager->removeUrlSuffix($rawPathInfo,$this->urlSuffix);

//URLsuffixrequired,butnotfoundintherequestedURL
if($manager->useStrictParsing&&$pathInfo===$rawPathInfo)
{
$urlSuffix=$this->urlSuffix===null?$manager->urlSuffix:$this->urlSuffix;
if($urlSuffix!=''&&$urlSuffix!=='/')
returnfalse;
}

if($this->hasHostInfo)
$pathInfo=strtolower($request->getHostInfo()).rtrim('/'.$pathInfo,'/');

$pathInfo.='/';

if(preg_match($this->pattern.$case,$pathInfo,$matches))
{
foreach($this->defaultParamsas$name=>$value)
{
if(!isset($_GET[$name]))
$_REQUEST[$name]=$_GET[$name]=$value;
}
$tr=array();
foreach($matchesas$key=>$value)
{
if(isset($this->references[$key]))
$tr[$this->references[$key]]=$value;
elseif(isset($this->params[$key]))
$_REQUEST[$key]=$_GET[$key]=$value;
}
if($pathInfo!==$matches[0])//there'readditionalGETparams
$manager->parsePathInfo(ltrim(substr($pathInfo,strlen($matches[0])),'/'));
if($this->routePattern!==null)
returnstrtr($this->route,$tr);
else
return$this->route;
}
else
returnfalse;
}

Parses a URL based on this rule.