Skip to content

Configuration

resource()

resource() — returns struct

Available in: mapper Category: Routing

Create a group of routes that exposes actions for manipulating a singular resource. A singular resource exposes URL patterns for the entire CRUD lifecycle of a single entity (show, new, create, edit, update, and delete) without exposing a primary key in the URL. Usually this type of resource represents a singleton entity tied to the session, application, or another resource (perhaps nested within another resource). If you need to generate routes for manipulating a collection of resources with a primary key in the URL, see the resources mapper method.

NameTypeRequiredDefaultDescription
namestringyes—Camel-case name of resource to reference when build links and form actions. This is typically a singular word (e.g., profile).
nestedbooleannofalseWhether or not additional calls will be nested within this resource.
pathstringno[runtime expression]Override URL path representing this resource. Default is a dasherized version of name (e.g., blogPost generates a path of blog-post).
controllerstringno—Override name of the controller used by resource. This defaults to a pluralized version of name.
singularstringno—Override singularize() result in plural resources.
pluralstringno—Override pluralize() result in singular resource.
onlystringno—Limits the list of RESTful routes to generate. Can include show, new, create, edit, update, and delete.
exceptstringno—Excludes RESTful routes to generate, taking priority over the only argument. Can include show, new, create, edit, update, and delete.
shallowbooleanno—Turn on shallow resources.
shallowPathstringno—Shallow path prefix.
shallowNamestringno—Shallow name prefix.
constraintsstructno—Variable patterns to use for matching.
callbackanyno—
bindinganyno—
bindByanyno—
$callstringnoresource
$pluralbooleannofalse
mapFormatbooleanno[runtime expression]Whether or not to add an optional .[format] pattern to the end of the generated routes. This is useful for providing formats via URL like json, xml, pdf, etc.
<cfscript>

mapper()
    // 1. Minimal singular resource — generates show, new, create, edit, update, delete routes
    .resource("checkout")

    // 2. Point to a controller at a custom path (app/controllers/sessions/Auth.cfc)
    .resource(name="auth", controller="sessions/auth")

    // 3. Limit generated routes with the `only` argument
    .resource(name="profile", only="show,edit,update")

    // 4. Exclude specific routes with the `except` argument
    .resource(name="cart", except="new,create")

    // 5. Nested singular resource — nest additional routes inside, then close with end()
    .resource(name="preferences", nested=true)
      .get(name="editPassword", to="passwords##edit")
      .patch(name="password", to="passwords##update")
      .resources("notifications")
    .end()

    // 6. Override the URL path (blogPostOptions -> blog-post/options instead of blog-post-options)
    .resource(name="blogPostOptions", path="blog-post/options")
.end();

</cfscript>