Configuration
namespace()
Signature
Section titled “Signature”namespace() — returns struct
Available in: mapper
Category: Routing
Description
Section titled “Description”The namespace() function in Wheels is used to group controllers and routes under a specific namespace (subfolder/package). It also prepends the namespace to route names and can modify the URL path. This is useful for organizing APIs, versioning, or modular applications. Namespaces can be nested for hierarchical routing, e.g., /api/v1/… and /api/v2/…
Parameters
Section titled “Parameters”| Name | Type | Required | Default | Description |
|---|---|---|---|---|
name | string | yes | — | Name to prepend to child route names. |
package | string | no | [runtime expression] | Subfolder (package) to reference for controllers. This defaults to the value provided for name. |
path | string | no | [runtime expression] | Subfolder path to add to the URL. |
Examples
Section titled “Examples”<cfscript>
mapper()
.namespace("api")
.namespace("v2")
// Route name: apiV2Products
// Example URL: /api/v2/products/1234
// Controller: api.v2.Products
.resources("products")
.end()
.namespace("v1")
// Route name: apiV1Users
// Example URL: /api/v1/users
// Controller: api.v1.Users
.get(name="users", to="users##index")
.end()
.end()
.namespace(name="foo", package="foos", path="foose")
// Route name: fooBars
// Example URL: /foose/bars
// Controller: foos.Bars
.post(name="bars", to="bars##create")
.end()
.end();
</cfscript>