Skip to content

Configuration

scope()

scope() — returns struct

Available in: mapper Category: Routing

Set any number of parameters to be inherited by mappers called within this matcher’s block. For example, set a package or URL path to be used by all child routes.

NameTypeRequiredDefaultDescription
namestringno—Name to prepend to child route names for use when building links, forms, and other URLs.
pathstringno—Path to prefix to all child routes.
packagestringno—Package namespace to append to controllers.
controllerstringno—Controller to use for routes.
shallowbooleanno—Turn on shallow resources to eliminate routing added before this one.
shallowPathstringno—Shallow path prefix.
shallowNamestringno—Shallow name prefix.
constraintsstructno—Variable patterns to use for matching.
middlewareanyno—
bindinganyno—
bindByanyno—
callbackanyno—A callback function to define nested routes within this scope. If provided, the scope is automatically closed when the callback completes.
$callstringnoscope
<cfscript>

mapper()
    // 1. Scope routes to a specific controller.
    // All routes inside will use the `freeForAll` controller.
    .scope(controller="freeForAll")
        .get(name="bananas", action="bananas")
        .root(action="index")
    .end()

    // 2. Scope routes to a package (subfolder) without affecting the URL.
    // All routes' controllers inside will be inside the `public` package/subfolder.
    .scope(package="public")
        .resource(name="search", only="show,create")
    .end()

    // 3. Scope routes under a URL path prefix.
    // All routes inside will be prepended with a URL path of `phones/`.
    .scope(path="phones")
        .get(name="newest", to="phones##newest")
        .get(name="sortOfNew", to="phones##sortOfNew")
    .end()

    // 4. Scope routes with both a name prefix and URL path prefix.
    // Generates named routes like `adminUsers` and `adminPosts`.
    .scope(name="admin", path="admin")
        .resources(name="users")
        .resources(name="posts")
    .end()

    // 5. Scope routes with URL variable constraints applied to all children.
    .scope(constraints={id="[0-9]+"})
        .get(name="userProfile", to="users##profile")
        .resources(name="orders")
    .end()
.end();

</cfscript>