Craft 3 Class Reference

Class craft\web\UrlManager

Inheritance
craft\web\UrlManager » yii\web\UrlManager » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Available since version
3.0
Source Code
https://github.com/craftcms/cms/blob/master/src/web/UrlManager.php

Public Properties
Property Type Description Defined By
$cache yii\caching\CacheInterface, string The cache object or the application component ID of the cache object. yii\web\UrlManager
$enablePrettyUrl boolean Whether to enable pretty URLs. yii\web\UrlManager
$enableStrictParsing boolean Whether to enable strict parsing. yii\web\UrlManager
$normalizer yii\web\UrlNormalizer, array, string, false The configuration for yii\web\UrlNormalizer used by this UrlManager. yii\web\UrlManager
$routeParam string The GET parameter name for route. yii\web\UrlManager
$ruleConfig array The default configuration of URL rules. yii\web\UrlManager
$rules array The rules for creating and parsing URLs when $enablePrettyUrl is true. yii\web\UrlManager
$showScriptName boolean Whether to show entry script name in the constructed URL. yii\web\UrlManager
$suffix string The URL suffix used when $enablePrettyUrl is true. yii\web\UrlManager
Protected Properties
Property Type Description Defined By
$cacheKey string The cache key for cached rules yii\web\UrlManager
Public Methods
Method Description Defined By
__call() Calls the named method which is not a class method. yii\base\BaseObject
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. craft\web\UrlManager
__get() Returns the value of an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__unset() Sets an object property to null. yii\base\BaseObject
addRules() Adds additional URL rules. yii\web\UrlManager
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
canGetProperty() Returns a value indicating whether a property can be read. yii\base\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
className() Returns the fully qualified name of this class. yii\base\BaseObject
createAbsoluteUrl() Creates an absolute URL using the given route and query parameters. craft\web\UrlManager
createUrl() Creates a URL using the given route and query parameters. craft\web\UrlManager
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
getBaseUrl() Returns the base URL that is used by createUrl() to prepend to created URLs. yii\web\UrlManager
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getHostInfo() Returns the host info that is used by createAbsoluteUrl() to prepend to created URLs. yii\web\UrlManager
getMatchedElement() Returns the element that was matched by the URI. craft\web\UrlManager
getRouteParams() Returns the route params, or null if we haven't parsed the URL yet. craft\web\UrlManager
getScriptUrl() Returns the entry script URL that is used by createUrl() to prepend to created URLs. yii\web\UrlManager
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
init() Initializes the object. yii\base\BaseObject
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
parseRequest() Parses the user request. craft\web\UrlManager
setBaseUrl() Sets the base URL that is used by createUrl() to prepend to created URLs. yii\web\UrlManager
setHostInfo() Sets the host info that is used by createAbsoluteUrl() to prepend to created URLs. yii\web\UrlManager
setRouteParams() Sets params to be passed to the routed controller action. craft\web\UrlManager
setScriptUrl() Sets the entry script URL that is used by createUrl() to prepend to created URLs. yii\web\UrlManager
trigger() Triggers an event. yii\base\Component
Protected Methods
Method Description Defined By
buildRules() Builds URL rule objects from the given rule declarations. craft\web\UrlManager
canBeCached() Returns the value indicating whether result of createUrl() of rule should be cached in internal cache. yii\web\UrlManager
getBuiltRulesFromCache() Provides the built URL rules that are associated with the $ruleDeclarations from cache. yii\web\UrlManager
getUrlFromCache() Get URL from internal cache if exists. yii\web\UrlManager
setBuiltRulesCache() Stores $builtRules to cache, using $rulesDeclaration as a part of cache key. yii\web\UrlManager
setRuleToCache() Store rule (e.g. yii\web\UrlRule) to internal cache. yii\web\UrlManager
Events
Event Type Description Defined By
EVENT_REGISTER_CP_URL_RULES craft\events\RegisterUrlRulesEvent The event that is triggered when registering URL rules for the Control Panel. craft\web\UrlManager
EVENT_REGISTER_SITE_URL_RULES craft\events\RegisterUrlRulesEvent The event that is triggered when registering URL rules for the front-end site. craft\web\UrlManager

Method Details

__construct() public method #

Constructor.

public void __construct ( array $config = [] )
$config array

buildRules() protected method #

Builds URL rule objects from the given rule declarations.

protected yii\web\UrlRuleInterface buildRules ( $rules )
$rules
return yii\web\UrlRuleInterface The rule objects built from the given rule declarations
throws yii\base\InvalidConfigException if a rule declaration is invalid

createAbsoluteUrl() public method #

Creates an absolute URL using the given route and query parameters.

This method prepends the URL created by createUrl() with the \craft\web\hostInfo.

Note that unlike yii\helpers\Url::toRoute(), this method always treats the given route as an absolute route.

public string createAbsoluteUrl ( $params, $scheme null )
$params string, array Use a string to represent a route (e.g. site/index), or an array to represent a route with query parameters (e.g. ['site/index', 'param1' => 'value1']).
$scheme string, null The scheme to use for the URL (either http, https or empty string for protocol-relative URL). If not specified the scheme of the current request will be used.
return string The created URL

createUrl() public method #

Creates a URL using the given route and query parameters.

You may specify the route as a string, e.g., site/index. You may also use an array if you want to specify additional query parameters for the URL being created. The array format must be:

// generates: /index.php?r=site%2Findex&param1=value1&param2=value2
['site/index', 'param1' => 'value1', 'param2' => 'value2']

If you want to create a URL with an anchor, you can use the array format with a # parameter. For example,

// generates: /index.php?r=site%2Findex&param1=value1#name
['site/index', 'param1' => 'value1', '#' => 'name']

The URL created is a relative one. Use createAbsoluteUrl() to create an absolute URL.

Note that unlike yii\helpers\Url::toRoute(), this method always treats the given route as an absolute route.

public string createUrl ( $params )
$params string, array Use a string to represent a route (e.g. site/index), or an array to represent a route with query parameters (e.g. ['site/index', 'param1' => 'value1']).
return string The created URL

getMatchedElement() public method #

Returns the element that was matched by the URI.

public craft\base\ElementInterface, false getMatchedElement ( )

getRouteParams() public method #

Returns the route params, or null if we haven't parsed the URL yet.

public array, null getRouteParams ( )

parseRequest() public method #

Parses the user request.

public array, boolean parseRequest ( $request )
$request yii\web\Request The request component
return array, boolean The route and the associated parameters. The latter is always empty if $enablePrettyUrl is false. false is returned if the current request cannot be successfully parsed.

setRouteParams() public method #

Sets params to be passed to the routed controller action.

public void setRouteParams ( array $params )
$params array

Event Details

EVENT_REGISTER_CP_URL_RULES event of type craft\events\RegisterUrlRulesEvent #

The event that is triggered when registering URL rules for the Control Panel.

This event gets called during class initialization, so you should always use a class-level event handler.

use craft\events\RegisterUrlRulesEvent;
use craft\web\UrlManager;
use yii\base\Event;
Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function(RegisterUrlRulesEvent $e) {
    $e->rules['foo'] => 'bar/baz';
});

EVENT_REGISTER_SITE_URL_RULES event of type craft\events\RegisterUrlRulesEvent #

The event that is triggered when registering URL rules for the front-end site.

This event gets called during class initialization, so you should always use a class-level event handler.

use craft\events\RegisterUrlRulesEvent;
use craft\web\UrlManager;
use yii\base\Event;
Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_SITE_URL_RULES, function(RegisterUrlRulesEvent $e) {
    $e->rules['foo'] => 'bar/baz';
});