Skip to content

Controller

filters()

filters() — returns void

Available in: controller Category: Configuration Functions

Tells Wheels to run a function before an action is run or after an action has been run.

NameTypeRequiredDefaultDescription
throughstringyes—Function(s) to execute before or after the action(s).
typestringnobeforeWhether to run the function(s) before or after the action(s).
onlystringno—Pass in a list of action names (or one action name) to tell Wheels that the filter function(s) should only be run on these actions.
exceptstringno—Pass in a list of action names (or one action name) to tell Wheels that the filter function(s) should be run on all actions except the specified ones.
placementstringnoappendPass in prepend to prepend the function(s) to the filter chain instead of appending.
// 1. Run `restrictAccess` before every action in this controller (declared inside config()).
filters("restrictAccess");

// 2. Run two before-filters on every action except `home` and `login`.
filters(through="isLoggedIn, checkIPAddress", except="home, login");

// 3. Run `auditLog` after only the `create`, `update`, and `delete` actions.
filters(through="auditLog", type="after", only="create, update, delete");

// 4. Prepend a filter so it runs before any already-registered filters.
filters(through="maintenanceCheck", placement="prepend");

// Note: filter functions must be declared as `private` in the controller
// to prevent them from being routed as public actions.
// Example controller setup:
// component extends="Controller" {
//     function config() {
//         filters("restrictAccess");
//     }
//     private function restrictAccess() {
//         if (!isLoggedIn()) {
//             redirectTo(route="login");
//         }
//     }
// }