Class CUrlManager
CUrlManager manages the URLs of Yii Web applications.
It provides URL construction (CUrlManager::createUrl()
) as well as parsing (CUrlManager::parseUrl()
) functionality.
- 'path' format: /path/to/EntryScript.php/name1/value1/name2/value2...
- 'get' format: /path/to/EntryScript.php?name1=value1&name2=value2...
- parse the requested URL into a route ('ControllerID/ActionID') and GET parameters;
- create URLs based on the given route and GET parameters.
A rule consists of a route and a pattern. The latter is used by CUrlManager to determine which rule is used for parsing/creating URLs. A pattern is meant to match the path info part of a URL. It may contain named parameters using the syntax '<ParamName:RegExp>'.
When parsing a URL, a matching rule will extract the named parameters from the path info and put them into the $_GET variable; when creating a URL, a matching rule will extract the named parameters from $_GET and put them into the path info part of the created URL.
If a pattern ends with '/*', it means additional GET parameters may be appended to the path info part of the URL; otherwise, the GET parameters can only appear in the query string part.
To specify URL rules, set the setRules rules property as an array of rules (pattern=>route). For example,
array( 'articles'=>'article/list', 'article/<id:\d+>/*'=>'article/read', )Two rules are specified in the above:
- The first rule says that if the user requests the URL '/path/to/index.php/articles', it should be treated as '/path/to/index.php/article/list'; and vice versa applies when constructing such a URL.
- The second rule contains a named parameter 'id' which is specified using the <ParamName:RegExp> syntax. It says that if the user requests the URL '/path/to/index.php/article/13', it should be treated as '/path/to/index.php/article/read?id=13'; and vice versa applies when constructing such a URL.
The route part may contain references to named parameters defined in the pattern part. This allows a rule to be applied to different routes based on matching criteria. For example,
array( '<_c:(post|comment)>/<id:\d+>/<_a:(create|update|delete)>'=>'<_c>/<_a>', '<_c:(post|comment)>/<id:\d+>'=>'<_c>/view', '<_c:(post|comment)>s/*'=>'<_c>/list', )
In the above, we use two named parameters '<_c>' and '<_a>' in the route part. The '<_c>' parameter matches either 'post' or 'comment', while the '<_a>' parameter matches an action ID.
Like normal rules, these rules can be used for both parsing and creating URLs. For example, using the rules above, the URL '/index.php/post/123/create' would be parsed as the route 'post/create' with GET parameter 'id' being 123. And given the route 'post/list' and GET parameter 'page' being 2, we should get a URL '/index.php/posts/page/2'.
It is also possible to include hostname into the rules for parsing and
creating URLs. One may extract part of the hostname to be a GET parameter. For
example, the URL http:<span
class="php-comment">//admin.example.com/en/profile</span>
may be
parsed into GET parameters user=admin
and lang=en
. On
the other hand, rules with hostname may also be used to create URLs with
parameterized hostnames.
In order to use parameterized hostnames, simply declare URL rules with host info, e.g.:
array( 'http://<user:\w+>.example.com/<lang:\w+>/profile' => 'user/profile', )
Starting from version 1.1.8, one can write custom URL rule classes and use them for one or several URL rules. For example,
array( // a standard rule '<action:(login|logout)>' => 'site/<action>', // a custom rule using data in DB array( 'class' => 'application.components.MyUrlRule', 'connectionID' => 'db', ), )Please note that the custom URL rule class should extend from
CBaseUrlRule
and implement the following two methods,
CUrlManager is a default application component that may be accessed via
CApplication::getUrlManager()
.
- CComponent
- CApplicationComponent implements IApplicationComponent
- CUrlManager
Direct known subclasses
X2UrlManagerCopyright: 2008-2013 Yii Software LLC
License: http://www.yiiframework.com/license/
Author: Qiang Xue <qiang.xue@gmail.com>
Since: 1.0
Located at x2engine/framework/web/CUrlManager.php
public
|
|
protected
|
|
public
|
#
addRules( array $rules, boolean $append = true )
Adds new URL rules. In order to make the new rules effective, this method
must be called BEFORE |
protected
|
#
createUrlRule( mixed $route, string $pattern )
Creates a URL rule instance. The default implementation returns a CUrlRule object. |
public
string
|
|
protected
string
|
#
createUrlDefault( string $route, array $params, string $ampersand )
Creates a URL based on default settings. |
public
string
|
|
public
|
#
parsePathInfo( string $pathInfo )
Parses a path info into URL segments and saves them to $_GET and $_REQUEST. |
public
string
|
#
createPathInfo( array $params, string $equal, string $ampersand, string $key = null )
Creates a path info based on the given parameters. |
public
string
|
|
public
string
|
|
public
|
#
setBaseUrl( string $value )
Sets the base URL of the application (the part after host name and before
query string). This method is provided in case the baseUrl cannot be
determined automatically. The ending slashes should be stripped off. And you are
also responsible to remove the script name if you set |
public
string
|
|
public
|
getIsInitialized()
|
string |
CACHE_KEY
|
'Yii.CUrlManager.rules' |
|
string |
GET_FORMAT
|
'get' |
|
string |
PATH_FORMAT
|
'path' |
public
array
|
$rules | array() |
#
the URL rules (pattern=>route). |
public
string
|
$urlSuffix | '' |
#
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. |
public
boolean
|
$showScriptName | true |
#
whether to show entry script name in the constructed URL. Defaults to true. |
public
boolean
|
$appendParams | true |
#
whether to append GET parameters to the path info part. Defaults to true. This property is only effective when 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
string
|
$routeVar | 'r' |
#
the GET variable name for route. Defaults to 'r'. |
public
boolean
|
$caseSensitive | true |
#
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 ( |
public
boolean
|
$matchValue | false |
#
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. |
public
string
|
$cacheID | 'cache' |
#
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
boolean
|
$useStrictParsing | false |
#
whether to enable strict URL parsing. This property is only effective when urlFormat is 'path'. If it is set true, then an incoming URL must match one of the rules URL rules. Otherwise, it will be treated as an invalid request and trigger a 404 HTTP exception. Defaults to false. |
public
string
|
$urlRuleClass | 'CUrlRule' |
#
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 |
$behaviors
|
public
string
|
$baseUrl |
#
The base URL of the application (the part after host name and before query
string). If |
public
string
|
$urlFormat |
#
The URL format. Defaults to 'path'. Valid values include 'path' and 'get'. Please refer to the guide for more details about the difference between these two formats. |
$isInitialized
|