Yii Framework v1.1.10 绫诲弬鑰

CUrlRule

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.

鍏叡灞炴

闅愯棌缁ф壙灞炴

灞炴绫诲瀷鎻忚堪瀹氫箟鍦
append boolean whether the URL allows additional parameters at the end of the path info. CUrlRule
caseSensitive boolean whether the rule is case sensitive. CUrlRule
defaultParams array the default GET parameters (name=>value) that this rule provides. CUrlRule
hasHostInfo boolean whether host info should be considered for this rule CUrlRule
matchValue boolean whether the GET parameter values should match the corresponding sub-patterns in the rule when creating a URL. CUrlRule
params array list of parameters (name=>regular expression) CUrlRule
parsingOnly boolean whether this rule is only used for request parsing. CUrlRule
pattern string regular expression used to parse a URL CUrlRule
references array the mapping from route param name to token name (e. CUrlRule
route string the controller/action pair CUrlRule
routePattern string the pattern used to match route CUrlRule
template string template used to construct a URL CUrlRule
urlSuffix string the URL suffix used for this rule. CUrlRule
verb string the 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() 璁$畻涓涓狿HP琛ㄨ揪寮忥紝鎴栨牴鎹粍浠朵笂涓嬫枃鎵ц鍥炶皟銆 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=><1>)

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)
$route string the route of the URL (controller/action)
$pattern string the pattern for matching the URL
婧愮爜锛 framework/web/CUrlManager.php#644 (鏄剧ず)
public function __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(
$tokens as $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)
        throw new 
CException(Yii::t('yii','The URL pattern "{pattern}" for route "{route}" is not a valid regular expression.',
            array(
'{route}'=>$route,'{pattern}'=>$pattern)));
}

Constructor.

createUrl() 鏂规硶
public mixed createUrl(CUrlManager $manager, string $route, array $params, string $ampersand)
$manager CUrlManager the manager
$route string the route
$params array list of parameters
$ampersand string the token separating name-value pairs in the URL.
{return} mixed the constructed URL or false on error
婧愮爜锛 framework/web/CUrlManager.php#712 (鏄剧ず)
public function createUrl($manager,$route,$params,$ampersand)
{
    if(
$this->parsingOnly)
        return 
false;

    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->references as $key=>$name)
                
$tr[$name]=$matches[$key];
        }
        else
            return 
false;
    }

    foreach(
$this->defaultParams as $key=>$value)
    {
        if(isset(
$params[$key]))
        {
            if(
$params[$key]==$value)
                unset(
$params[$key]);
            else
                return 
false;
        }
    }

    foreach(
$this->params as $key=>$value)
        if(!isset(
$params[$key]))
            return 
false;

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

    foreach(
$this->params as $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)
$manager CUrlManager the URL manager
$request CHttpRequest the request object
$pathInfo string path info part of the URL
$rawPathInfo string path info that contains the potential URL suffix
{return} mixed the route that consists of the controller ID and action ID or false on error
婧愮爜锛 framework/web/CUrlManager.php#798 (鏄剧ず)
public function parseUrl($manager,$request,$pathInfo,$rawPathInfo)
{
    if(
$this->verb!==null && !in_array($request->getRequestType(), $this->verbtrue))
        return 
false;

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

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

    
// URL suffix required, but not found in the requested URL
    
if($manager->useStrictParsing && $pathInfo===$rawPathInfo)
    {
        
$urlSuffix=$this->urlSuffix===null $manager->urlSuffix $this->urlSuffix;
        if(
$urlSuffix!='' && $urlSuffix!=='/')
            return 
false;
    }

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

    
$pathInfo.='/';

    if(
preg_match($this->pattern.$case,$pathInfo,$matches))
    {
        foreach(
$this->defaultParams as $name=>$value)
        {
            if(!isset(
$_GET[$name]))
                
$_REQUEST[$name]=$_GET[$name]=$value;
        }
        
$tr=array();
        foreach(
$matches as $key=>$value)
        {
            if(isset(
$this->references[$key]))
                
$tr[$this->references[$key]]=$value;
            else if(isset(
$this->params[$key]))
                
$_REQUEST[$key]=$_GET[$key]=$value;
        }
        if(
$pathInfo!==$matches[0]) // there're additional GET params
            
$manager->parsePathInfo(ltrim(substr($pathInfo,strlen($matches[0])),'/'));
        if(
$this->routePattern!==null)
            return 
strtr($this->route,$tr);
        else
            return 
$this->route;
    }
    else
        return 
false;
}

Parses a URL based on this rule.

Copyright © 2008-2011 by Yii Software LLC
All Rights Reserved.