Skip to content

Global Helpers

URLFor()

URLFor() — returns string

Available in: controller, model, test, migrator, migration, tabledefinition Category: Miscellaneous Functions

Generates an internal URL based on the supplied arguments. It can create URLs using a named route, or by specifying a controller and action directly. Additional options let you include keys, query parameters, anchors, and override protocol, host, or port. By default, the function returns a relative URL, but you can configure it to return a fully qualified URL. URL parameters are automatically encoded for safety, but for HTML attribute safety, further encoding is recommended.

NameTypeRequiredDefaultDescription
routestringnoName of a route that you have configured in config/routes.cfm.
controllerstringnoName of the controller to include in the URL.
actionstringnoName of the action to include in the URL.
keyanynoKey(s) to include in the URL.
paramsstringnoAny additional parameters to be set in the query string (example: wheels=cool&x=y). Please note that Wheels uses the & and = characters to split the parameters and encode them properly for you. However, if you need to pass in & or = as part of the value, then you need to encode them (and only them), example: a=cats%26dogs%3Dtrouble!&b=1.
anchorstringnoSets an anchor name to be appended to the path.
onlyPathbooleannotrueIf true, returns only the relative URL (no protocol, host name or port).
hoststringnoSet this to override the current host.
protocolstringnoSet this to override the current protocol.
portnumericno0Set this to override the current port number.
encodebooleannotrueEncode URL parameters using EncodeForURL(). Please note that this does not make the string safe for placement in HTML attributes, for that you need to wrap the result in EncodeForHtmlAttribute() or use linkTo(), startFormTag() etc instead.
$encodeForHtmlAttributebooleannofalse
$URLRewritingstringno[runtime expression]
1. Create the URL for the `logOut` action on the `account` controller, typically resulting in `/account/log-out`
#urlFor(controller="account", action="logOut")#

2. Create a URL with an anchor set on it
#urlFor(action="comments", anchor="comment10")#

3. Create a URL based on a route called `products`, which expects params for `categorySlug` and `productSlug`
#urlFor(route="product", categorySlug="accessories", productSlug="battery-charger")#