LogoLogo
HomeAPIBlog
3.0.0-SNAPSHOT
3.0.0-SNAPSHOT
  • INTRODUCTION
    • Getting Started
      • Running Local Development Servers
      • Beginner Tutorial: Hello World
      • Beginner Tutorial: Hello Database
      • Tutorial: Wheels, AJAX, and You
    • Frameworks and Wheels
    • Requirements
    • Manual Installation
    • Upgrading
    • Screencasts
  • Command Line Tools
    • CLI Commands
    • wheels - commands
    • wheels generate - commands
    • wheels dbmigrate - commands
    • wheels plugins - commands
  • Working with Wheels
    • Conventions
    • Configuration and Defaults
    • Directory Structure
    • Switching Environments
    • Testing Your Application
    • Contributing to Wheels
    • Documenting your Code
  • Handling Requests with Controllers
    • Request Handling
    • Rendering Content
    • Redirecting Users
    • Sending Files
    • Sending Email
    • Responding with Multiple Formats
    • Using the Flash
    • Using Filters
    • Verification
    • Event Handlers
    • Routing
    • URL Rewriting
      • Apache
      • IIS
      • Tomcat
      • Nginx
    • Obfuscating URLs
    • Caching
    • Nesting Controllers
    • CORS Requests
  • Displaying Views to Users
    • Pages
    • Partials
    • Linking Pages
    • Layouts
    • Form Helpers and Showing Errors
    • Displaying Links for Pagination
    • Date, Media, and Text Helpers
    • Creating Custom View Helpers
    • Localization
  • Database Interaction Through Models
    • Object Relational Mapping
    • Creating Records
    • Reading Records
    • Updating Records
    • Deleting Records
    • Column Statistics
    • Dynamic Finders
    • Getting Paginated Data
    • Associations
    • Nested Properties
    • Object Validation
    • Object Callbacks
    • Calculated Properties
    • Transactions
    • Dirty Records
    • Soft Delete
    • Automatic Time Stamps
    • Database Migrations
      • Migrations in Production
    • Using Multiple Data Sources
  • Plugins
    • Installing and Using Plugins
    • Developing Plugins
    • Publishing Plugins
  • Project Documentation
    • Overview
  • External Links
    • Source Code
    • Issue Tracker
    • Sponsor Us
    • Community
Powered by GitBook
LogoLogo
On this page
  • A Handy Mapping
  • Not just controllers...

Was this helpful?

Edit on GitHub
Export as PDF
  1. Handling Requests with Controllers

Nesting Controllers

With the new routing system in Wheels 2.x, there are lots of nice features which allow for better code organization. One of these is the ability to nest controllers into folders using the namespace() method in our mapper() call.

For example, we may have a whole "Admin" section, where for each endpoint, we need to check some permissions, and possibly load some default data. Let's say we have a Users controller which provides standard CRUD operations.

app/config/routes.cfm
.mapper()
  .namespace("admin")
    .resources("users")
  .end()
.end()

This will automatically look for the Users.cfc controller in app/controllers/admin/.

By default, all your controllers extend="Controller", but with a nested controller, we need to change this, as the main Controller.cfc isn't at the same folder level.

A Handy Mapping

We've added a new mapping in 3.x, called app; This mapping will correspond to the app folder, so in our Users.cfc we now have two options - extend the core Controller.cfc via the app mapping, or perhaps extend another component (possibly Admin.cfc) which extends the core Controller instead.

app/controllers/admin/Users.cfc
component extends="app.controllers.Controller" {

  function config(){
    super.config();
  }

}

In the above example, we're using the app mapping to "go to" the app folder, and then look for a folder called controllers, and within that, our main Controller.cfc.

Our super.config() call will then run the config() function in our base Controller.

We could of course have the following too (just for completeness sake):

File system
/app/
  /controllers/
    /admin/
      - Admin.cfc
      - Users.cfc
    /public/
      - etc.

And then add the app.controllers.Controller mapping to Admin.cfc, and the extends="Admin" in the Users.cfc.

Not just controllers...

Of course, we can extend this concept (ha!) to Models too. However, this is either limited to tableless models, or models where you implicitly specify the table() call. As Wheels will look for the tablename dependent on the model file location, it'll get confused if in a sub-directory.

app/models/auth/LDAP.cfc
component extends="app.models.Model"
{
    function config() {
        table(false);
    }
    function save(){
    }
}

It also potentially makes your model() calls more complex, as you need to specify the model name in dot notation:

Example nested model call
// Example for "LDAP.cfc" in "/models/auth"
myNewLDAPModel=model("auth.LDAP").new();
PreviousCachingNextCORS Requests

Last updated 21 days ago

Was this helpful?