Skip to content

Configuration

root()

root() — returns struct

Available in: mapper Category: Routing

Defines a route that matches the root of the current context. This could be the root of the entire application (like the home page) or the root of a namespaced section of your routes. It is commonly used to map a controller action to the main entry point of your application or a subsection of it. You can specify the controller and action either using the to argument (controller##action) or by passing controller and action separately. Optionally, mapFormat can be set to true to allow a format suffix like .json or .xml in the URL.

NameTypeRequiredDefaultDescription
tostringnoSet controller##action combination to map the route to. You may use either this argument or a combination of controller and action.
mapFormatbooleannoSet to true to include the format (e.g. .json) in the route.
1. Application Home Page
Map the root of the application (/) to a controller action:

<cfscript>
mapper()
    .root(to="dashboards##show")
.end();
</cfscript>

2. Root of a Namespaced Section (API)
Map /api to an API controller:

<cfscript>
mapper()
    .namespace("api")
        .root(controller="apis", action="index")
    .end();
</cfscript>

3. Root with Optional Format
Enable clients to request JSON or XML directly:

<cfscript>
mapper()
    .namespace("api")
        .root(controller="apis", action="index", mapFormat=true)
    .end();
</cfscript>

4. Root for Nested Resources
Use root() inside a nested scope:

<cfscript>
mapper()
    .namespace("admin")
        .root(controller="dashboard", action="index")
    .end();
.end();
</cfscript>