0 follower

CUrlRule

Package system.web
Inheritance class CUrlRule » CComponent
Since 1.0
Version $Id$
Source Code 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.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
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
params array list of parameters (name=>regular expression) CUrlRule
pattern string regular expression used to parse a URL CUrlRule
references array the mapping from route param name to token name (e.g. _r1=><1>) 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

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. CComponent
__construct() Constructor. CUrlRule
__get() Returns a property value, an event handler list or a behavior based on its name. CComponent
__isset() Checks if a property value is null. CComponent
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. CComponent
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
createUrl() Creates a URL based on this rule. CUrlRule
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. CComponent
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. CComponent
parseUrl() Parases a URL based on this rule. CUrlRule
raiseEvent() Raises an event. CComponent

Property Details

append property
public boolean $append;

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

caseSensitive property (available since v1.0.1)
public boolean $caseSensitive;

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

defaultParams property (available since v1.0.8)
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 property (available since v1.0.11)
public boolean $hasHostInfo;

whether host info should be considered for this rule

params property
public array $params;

list of parameters (name=>regular expression)

pattern property
public string $pattern;

regular expression used to parse a URL

references property (available since v1.0.5)
public array $references;

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

route property
public string $route;

the controller/action pair

routePattern property (available since v1.0.5)
public string $routePattern;

the pattern used to match route

template property
public string $template;

template used to construct a URL

urlSuffix property (available since v1.0.6)
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.

Method Details

__construct() method
public void __construct(string $route, string $pattern)
$route string the route of the URL (controller/action)
$pattern string the pattern for matching the URL
Source Code: framework/web/CUrlManager.php#471 (show)
public function __construct($route,$pattern)
{
    if(
is_array($route))
    {
        if(isset(
$route['urlSuffix']))
            
$this->urlSuffix=$route['urlSuffix'];
        if(isset(
$route['caseSensitive']))
            
$this->caseSensitive=$route['caseSensitive'];
        if(isset(
$route['defaultParams']))
            
$this->defaultParams=$route['defaultParams'];
        
$route=$this->route=$route[0];
    }
    else
        
$this->route=$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(
preg_match_all('/<(\w+):?(.*?)?>/',$pattern,$matches))
    {
        
$tokens=array_combine($matches[1],$matches[2]);
        foreach(
$tokens as $name=>$value)
        {
            
$tr["<$name>"]="(?P<$name>".($value!==''?$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() method
public string 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} string the constructed URL
Source Code: framework/web/CUrlManager.php#534 (show)
public function createUrl($manager,$route,$params,$ampersand)
{
    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->params as $key=>$value)
        if(!isset(
$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(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() method
public string 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} string the route that consists of the controller ID and action ID
Source Code: framework/web/CUrlManager.php#588 (show)
public function parseUrl($manager,$request,$pathInfo,$rawPathInfo)
{
    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!=='/')
            throw new 
CHttpException(404,Yii::t('yii','Unable to resolve the request "{route}".',
                array(
'{route}'=>$rawPathInfo)));
    }

    if(
$this->hasHostInfo)
        
$pathInfo=$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
            
CUrlManager::parsePathInfo(ltrim(substr($pathInfo,strlen($matches[0])),'/'));
        if(
$this->routePattern!==null)
            return 
strtr($this->route,$tr);
        else
            return 
$this->route;
    }
    else
        return 
false;
}

Parases a URL based on this rule.